0
0
Flaskframework~10 mins

Flask session object - 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 [1]
Drag options to blanks, or click blank then click option'
Arender_template
Bredirect
Crequest
Dsession
Attempts:
3 left
💡 Hint
Common Mistakes
Importing request instead of session
Using render_template instead of session
Forgetting to import session
2fill in blank
medium

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

Flask
session[[1]] = 'user123'
Drag options to blanks, or click blank then click option'
Ausername
B'username'
C'user_id'
Duser
Attempts:
3 left
💡 Hint
Common Mistakes
Using variable names without quotes as keys
Using keys like user without quotes
Forgetting quotes causes errors
3fill in blank
hard

Fix the error in the code to retrieve a session value safely.

Flask
user = session.get([1], None)
Drag options to blanks, or click blank then click option'
Ausername
B'user'
C'username'
Duser
Attempts:
3 left
💡 Hint
Common Mistakes
Using unquoted keys causing KeyError
Using wrong key names
Not providing a default value
4fill in blank
hard

Fill both blanks to check if a user is logged in using the session.

Flask
if [1] in session and session[[2]] == 'admin':
    print('Admin user')
Drag options to blanks, or click blank then click option'
A'username'
B'user'
C'admin'
D'role'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different keys in the condition
Forgetting quotes around keys
Checking wrong keys like 'username'
5fill in blank
hard

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

Flask
session.[1]()
return [2](url_for([3]))
Drag options to blanks, or click blank then click option'
Aclear
Bredirect
C'home'
Durl_for
Attempts:
3 left
💡 Hint
Common Mistakes
Using session.pop instead of clear for all keys
Forgetting to import redirect or url_for
Passing url_for as string instead of calling it