Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to import the session object from Flask.
Flask
from flask import [1]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Importing request instead of session
Using render_template instead of session
Forgetting to import session
✗ Incorrect
The session object is imported from Flask to manage user sessions.
2fill in blank
mediumComplete the code to set a value in the Flask session.
Flask
session[[1]] = 'user123'
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using variable names without quotes as keys
Using keys like user without quotes
Forgetting quotes causes errors
✗ Incorrect
Session keys must be strings, so use quotes around the key name.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using unquoted keys causing KeyError
Using wrong key names
Not providing a default value
✗ Incorrect
The key must be a string in quotes to access the session dictionary correctly.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different keys in the condition
Forgetting quotes around keys
Checking wrong keys like 'username'
✗ Incorrect
Check if the key 'user' exists in session and compare the 'role' value to 'admin'.
5fill in blank
hardFill 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'
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
✗ Incorrect
Use session.clear() to remove all session data, then redirect to the 'home' route.