Complete the code to import the Flask class.
from flask import [1] app = [1](__name__)
The Flask class is imported to create the app instance.
Complete the code to protect a route with a login check.
@app.route('/dashboard') def dashboard(): if not session.get('[1]'): return 'Please log in first', 401 return 'Welcome to your dashboard!'
The 'logged_in' key in session is commonly used to check if a user is logged in.
Fix the error in the login function to set the session correctly.
from flask import session def login(): # after verifying user credentials session['[1]'] = True return 'Logged in successfully!'
Setting 'logged_in' to True in session marks the user as logged in.
Fill both blanks to create a dictionary comprehension that stores usernames and their login status.
user_status = {user: session.get('[1]', False) for user in users if user [2] 'admin'}The comprehension checks if users are logged in and excludes 'admin' users.
Fill all three blanks to create a secure logout function that clears the session and redirects.
from flask import session, redirect, url_for def logout(): session.[1]() return redirect(url_for('[2]')) @app.route('/logout') def logout_route(): return logout()
Calling session.clear() removes all session data. Redirecting to 'login' page after logout is common.
