Hi,
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.
-
The first is that the Home button (in the navigation bar) does not lead to the Home page.
-
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 routes.py) may be responsible for the error.
Here is the code for each:
base.html
html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body {
background-color: #E8DCD9;
font-family: cursive;
}
.blue{
color:#187bcd
}
.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;
}
}
</style>
{% if title %}
<title>{{ title }} - TriPlanned</title>
{% else %}
<title>Welcome to TriPlanned</title>
{% endif %}
</head>
<body>
<div>
<center><h3 class="blue">TriPlanned</h3></center>
<center class="glow">🌴</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 %}
</div>
<hr>
{% with messages = get_flashed_messages() %}
{% if messages %}
<ul>
{% for message in messages %}
{% endfor %}
</ul>
{% endif %}
{% endwith %}
{% block content %}{% endblock %}
</body>
</html>
routes.py
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(username=form.username.data).first()
if user is None or not user.check_password(form.password.data):
flash('Invalid username or password')
return redirect(url_for('login'))
login_user(user, remember=form.remember_me.data)
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(username=form.username.data, email=form.email.data)
user.set_password(form.password.data)
db.session.add(user)
db.session.commit()
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'])
@login_required
def user(username):
user = current_user
user = User.query.filter_by(username=user.username).first()
posts = Post.query.filter_by(user_id=user.id)
if posts is None:
posts = []
form = DestinationForm()
if request.method == 'POST' and form.validate():
new_destination = Post(city = form.city.data,country=form.country.data,description=form.description.data, user_id=current_user.id)
db.session.add(new_destination)
db.session.commit()
else:
flash(form.errors)
destinations = Destination.query.all()
return render_template('user.html', user=user, posts=posts, form=form)
@app.route('/logout')
def logout():
logout_user()
return redirect(url_for('login'))
@app.route('/')
def index():
#posts = Post.query.all()
#if not posts:
#posts=[]
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 routes.py 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!