0
0
Flaskframework~10 mins

Session data storage in Flask - Interactive Code Practice

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

Complete the code to import the session object from Flask.

Flask
from flask import Flask, [1]
Drag options to blanks, or click blank then click option'
Asession
Brender_template
Crequest
Dredirect
Attempts:
3 left
💡 Hint
Common Mistakes
Importing request instead of session.
Forgetting to import session.
2fill in blank
medium

Complete the code to set a secret key needed for session management.

Flask
app = Flask(__name__)
app.secret_key = [1]
Drag options to blanks, or click blank then click option'
A'mysecretkey'
B12345
CNone
DTrue
Attempts:
3 left
💡 Hint
Common Mistakes
Using a number instead of a string for the secret key.
Setting the secret key to None or True.
3fill in blank
hard

Fix the error in the code to store a username in the session.

Flask
from flask import Flask, session
app = Flask(__name__)
app.secret_key = 'key'

@app.route('/login')
def login():
    session[1]['username'] = 'Alice'
    return 'Logged in'
Drag options to blanks, or click blank then click option'
A.add
B[
C.set
D.update
Attempts:
3 left
💡 Hint
Common Mistakes
Using dot notation like session.set which is invalid.
Trying to use session.add which does not exist.
4fill in blank
hard

Fill both blanks to check if 'username' is in session and retrieve it safely.

Flask
from flask import session

if [1] in session:
    user = session.[2]('username')
else:
    user = 'Guest'
Drag options to blanks, or click blank then click option'
A'username'
Bget
Cpop
Dusername
Attempts:
3 left
💡 Hint
Common Mistakes
Checking for variable username instead of string 'username'.
Using pop which removes the key instead of just getting it.
5fill in blank
hard

Fill all three blanks to clear the session and redirect to home page.

Flask
from flask import session, redirect, url_for

@app.route('/logout')
def logout():
    session.[1]()
    return [2]( [3]('home') )
Drag options to blanks, or click blank then click option'
Aclear
Bredirect
Curl_for
Dpop
Attempts:
3 left
💡 Hint
Common Mistakes
Using pop without a key to clear session.
Forgetting to use redirect or url_for.