February 2018
Intermediate to advanced
456 pages
9h 56m
English
We now need to implement the ability for users to log out. In web_server.py, add the following logout function endpoint:
@app.route('/logout')
def logout():
session.clear()
return redirect(url_for('home'))
If a user hits this endpoint, Flask will clear their session object and redirect them to the home endpoint. Since the session is cleared, the authenticated Boolean will be deleted.
In home.html, let's update our page to include the link for users to log out. To do this, we will add a new link just after our postMessage form:
{% if authenticated %} <form action="/messages" id="postMessage"> <input type="text" name="message" placeholder="Post message"> <input type="submit" value="Post"> </form> <p><a href="/logout">Logout</a></p> ...Read now
Unlock full access