Travel Sites Flask - Build Python Web Apps with Flask


I’m currently doing the Flask exercise titled Travel Sites. Having gone through most of the tasks (on Step 25 of 25), the final website I have doesn’t seem to work.
Namely, there are two key issues.

  1. The first is that the Home button (in the navigation bar) does not lead to the Home page.

  2. A similar problem persists with the Login button. Fortunately, the link there does work, but leads to an error saying the following:
    *RuntimeError: A secret key is required to use CSRF. *
    When I try to look at the code in more detail, the console becomes locked, asking me for a PIN (which I don’t know).

I think that two of the files (base.html and may be responsible for the error.

Here is the code for each:


<meta name="viewport" content="width=device-width, initial-scale=1">
body {
  background-color: #E8DCD9;
  font-family: cursive;
.glow {
  font-size: 30px;
  color: blue;
  text-align: center;
  -webkit-animation: glow 1s ease-in-out infinite alternate;
  -moz-animation: glow 1s ease-in-out infinite alternate;
  animation: glow 1s ease-in-out infinite alternate;
@-webkit-keyframes glow {
  from {
    text-shadow: 0 0 10px #fff, 0 0 20px #fff, 0 0 30px #e60073, 0 0 40px #e60073, 0 0 50px #e60073, 0 0 60px #e60073, 0 0 70px #e60073;
  to {
    text-shadow: 0 0 20px #fff, 0 0 30px #ff4da6, 0 0 40px #ff4da6, 0 0 50px #ff4da6, 0 0 60px #ff4da6, 0 0 70px #ff4da6, 0 0 80px #ff4da6;
        {% if title %}
        <title>{{ title }} - TriPlanned</title>
        {% else %}
        <title>Welcome to TriPlanned</title>
        {% endif %}
      <center><h3 class="blue">TriPlanned</h3></center>
      <center class="glow">&#127796;</center>
      <a class="blue pull-left" href="{{ url_for('index') }}">Home</a>
      {% if current_user.is_anonymous %}
      <a class="blue pull-right" href="{{ url_for('login') }}">Login</a>
      {% else %}
      <a class="blue pull-right" href="{{ url_for('user', username=current_user.username) }}">Profile</a>
      <a class="blue pull-left" href="{{ url_for('logout') }}">Logout</a>
      {% endif %}
        {% with messages = get_flashed_messages() %}
        {% if messages %}
            {% for message in messages %}
            {% endfor %}
        {% endif %}
        {% endwith %}
        {% block content %}{% endblock %}

from app import app
from flask import request, render_template, flash, redirect,url_for
from models import User, Post
from forms import RegistrationForm,LoginForm, DestinationForm
from werkzeug.urls import url_parse
from flask_login import current_user, login_user, logout_user, login_required
@app.route('/login', methods=['GET', 'POST'])
def login():
  if current_user.is_authenticated:
    return redirect(url_for('index'))
  form = LoginForm()
  if form.validate_on_submit():
    user = User.query.filter_by(
    if user is None or not user.check_password(
      flash('Invalid username or password')
      return redirect(url_for('login'))
    next_page = request.args.get('next')
    if not next_page or url_parse(next_page).netloc != '':
      next_page = url_for('index')
    return redirect(next_page)
  return render_template('login.html', title='Sign In', form=form)

@app.route('/register', methods=['GET', 'POST'])
def register():
  if current_user.is_authenticated:
    return redirect(url_for('index'))
  form = RegistrationForm()
  if form.validate_on_submit():
    user = User(,
    flash('Congratulations, you are now a registered user!')
    return redirect(url_for('login'))
  return render_template('register.html', title='Register', form=form)

@app.route('/user/<username>',methods=['GET', 'POST'])
def user(username):
  user = current_user
  user = User.query.filter_by(username=user.username).first()
  posts = Post.query.filter_by(
  if posts is None:
    posts = []
  form = DestinationForm()
  if request.method == 'POST' and form.validate():
    new_destination = Post(city =,,,
  destinations = Destination.query.all()
  return render_template('user.html', user=user, posts=posts, form=form)

def logout():
  return redirect(url_for('login'))

def index():
  #posts = Post.query.all()
  #if not posts:
  return render_template('landing_page.html',posts=posts)

My presumption is that something is not quite right with the index function at the bottom of and that there is an error related to the calling of form = LoginForm(). There is potentially a missing <a> tag as well somewhere.

The exercise’s URL is the following:

Thank you!

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.