0
0
Flaskframework~10 mins

Why sessions manage user state in Flask - Test Your Understanding

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the Flask session object.

Flask
from flask import Flask, [1]
Drag options to blanks, or click blank then click option'
Arequest
Bredirect
Csession
Drender_template
Attempts:
3 left
💡 Hint
Common Mistakes
Importing 'request' instead of 'session'.
Forgetting to import 'session' and trying to use it directly.
2fill in blank
medium

Complete the code to set a value in the session dictionary.

Flask
session[[1]] = 'user123'
Drag options to blanks, or click blank then click option'
A'token'
B'password'
C'email'
D'username'
Attempts:
3 left
💡 Hint
Common Mistakes
Using keys like 'password' which should not be stored in session.
Using keys unrelated to user identity.
3fill in blank
hard

Fix the error in the code to retrieve the username from the session safely.

Flask
user = session.get([1], None)
Drag options to blanks, or click blank then click option'
A'username'
B'user'
C'id'
D'name'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different key than what was stored causes None to be returned.
Not using get and risking a KeyError.
4fill in blank
hard

Fill both blanks to check if a user is logged in and redirect if not.

Flask
if [1] not in session:
    return [2]('/login')
Drag options to blanks, or click blank then click option'
A'username'
Bredirect
Crender_template
D'user_id'
Attempts:
3 left
💡 Hint
Common Mistakes
Checking the wrong key in session.
Using render_template instead of redirect.
5fill in blank
hard

Fill all three blanks to clear the session and log the user out.

Flask
from flask import [1]

@app.route('/logout')
def logout():
    [2].clear()
    return [3]('/login')
Drag options to blanks, or click blank then click option'
Asession
Credirect
Drequest
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to import session.
Using request instead of session.
Not redirecting after clearing session.