Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to import the Flask class.
Flask
from flask import [1] app = [1](__name__)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Request' instead of 'Flask' to create the app.
✗ Incorrect
The Flask class is imported to create the app instance.
2fill in blank
mediumComplete the code to protect a route with a login check.
Flask
@app.route('/dashboard') def dashboard(): if not session.get('[1]'): return 'Please log in first', 401 return 'Welcome to your dashboard!'
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'user' which may store username but not login status.
✗ Incorrect
The 'logged_in' key in session is commonly used to check if a user is logged in.
3fill in blank
hardFix the error in the login function to set the session correctly.
Flask
from flask import session def login(): # after verifying user credentials session['[1]'] = True return 'Logged in successfully!'
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Setting 'user' or 'username' to True instead of a boolean flag.
✗ Incorrect
Setting 'logged_in' to True in session marks the user as logged in.
4fill in blank
hardFill both blanks to create a dictionary comprehension that stores usernames and their login status.
Flask
user_status = {user: session.get('[1]', False) for user in users if user [2] 'admin'} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' instead of '!=' to exclude 'admin'.
✗ Incorrect
The comprehension checks if users are logged in and excludes 'admin' users.
5fill in blank
hardFill all three blanks to create a secure logout function that clears the session and redirects.
Flask
from flask import session, redirect, url_for def logout(): session.[1]() return redirect(url_for('[2]')) @app.route('/logout') def logout_route(): return logout()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using session.remove() which does not exist.
✗ Incorrect
Calling session.clear() removes all session data. Redirecting to 'login' page after logout is common.