0
0
Flaskframework~10 mins

Login_required decorator in Flask - 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 decorator function from Flask-Login.

Flask
from flask_login import [1]
Drag options to blanks, or click blank then click option'
Acurrent_user
BLoginManager
Clogin_required
Dlogout_user
Attempts:
3 left
💡 Hint
Common Mistakes
Importing LoginManager instead of login_required
Using current_user as a decorator
Forgetting to import login_required
2fill in blank
medium

Complete the code to protect the route so only logged-in users can access it.

Flask
@[1]
def dashboard():
    return "Welcome to your dashboard!"
Drag options to blanks, or click blank then click option'
Aapp.route('/dashboard')
Blogin_required
Croute('/dashboard')
Drequires_login
Attempts:
3 left
💡 Hint
Common Mistakes
Using app.route instead of login_required
Adding parentheses like login_required() incorrectly
Using a non-existent decorator like requires_login
3fill in blank
hard

Fix the error in the decorator usage to properly protect the route.

Flask
@[1]
def profile():
    return "User profile page"
Drag options to blanks, or click blank then click option'
Alogin_required(False)
Blogin_required()
Clogin_required(True)
Dlogin_required
Attempts:
3 left
💡 Hint
Common Mistakes
Adding parentheses when not required
Passing arguments to login_required which it does not accept
Using incorrect decorator syntax
4fill in blank
hard

Fill both blanks to create a route '/settings' that requires login and returns a simple message.

Flask
@[1]
@[2]('/settings')
def settings():
    return "Settings page"
Drag options to blanks, or click blank then click option'
Alogin_required
Bapp.route
Croute
Dlogin_required()
Attempts:
3 left
💡 Hint
Common Mistakes
Using login_required() with parentheses
Using route instead of app.route
Reversing the order of decorators
5fill in blank
hard

Fill all three blanks to create a Flask app with a protected route '/profile' that returns a welcome message.

Flask
from flask import Flask
from flask_login import [1], LoginManager

app = Flask(__name__)
login_manager = LoginManager()
login_manager.init_app(app)

@app.route('/profile')
@[2]
def profile():
    return "Welcome, user!"

if __name__ == '__main__':
    app.run(debug=[3])
Drag options to blanks, or click blank then click option'
Alogin_required
Blogin_required()
CTrue
DFalse
Attempts:
3 left
💡 Hint
Common Mistakes
Using login_required() with parentheses in code
Setting debug to False accidentally
Forgetting to import login_required