Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to import the Flask-Login extension.
Flask
from flask_login import [1]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect class names like FlaskLogin or UserLogin.
✗ Incorrect
The LoginManager class is imported from flask_login to manage user sessions.
2fill in blank
mediumComplete the code to initialize the LoginManager with the Flask app.
Flask
login_manager = [1]()
login_manager.init_app(app) Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong class names like UserManager or AuthManager.
✗ Incorrect
The LoginManager instance is created and linked to the Flask app to manage login sessions.
3fill in blank
hardFix the error in the user loader function decorator.
Flask
@login_manager.[1] def load_user(user_id): return User.get(user_id)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '@login_manager.load_user_loader' or '@login_manager.load_user'.
✗ Incorrect
The correct decorator is @login_manager.user_loader to register the user loader callback.
4fill in blank
hardFill both blanks to protect a route so only logged-in users can access it.
Flask
@app.route('/dashboard') @[1] def dashboard(): return 'Welcome to your dashboard!'
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '@login_manager' or '@user_loader' as decorators for routes.
✗ Incorrect
The @login_required decorator restricts access to logged-in users only.
5fill in blank
hardFill all three blanks to log in a user after verifying credentials.
Flask
from flask_login import [1] @app.route('/login', methods=['POST']) def login(): user = User.query.filter_by(username=request.form['username']).first() if user and user.check_password(request.form['password']): [2](user) return redirect([3]) return 'Invalid credentials', 401
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'logout_user' instead of 'login_user'.
Not redirecting after login.
✗ Incorrect
Import login_user to log in the user, call login_user(user), and redirect to the dashboard page.