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
Importing Request instead of Flask
Using render_template here
✗ 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 user_is_authenticated(): return [1]('/login') return 'Welcome to your dashboard!'
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using render_template instead of redirect
Using abort without explanation
✗ Incorrect
Redirect sends the user to the login page if not authenticated.
3fill in blank
hardFix the error in the authorization check to abort with 403 if unauthorized.
Flask
from flask import abort def admin_panel(): if not current_user.is_admin: [1](403) return 'Admin content here.'
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using redirect instead of abort
Calling login_required as a function here
✗ Incorrect
abort(403) stops the request and shows a forbidden error.
4fill in blank
hardFill both blanks to check if user role is 'editor' and allow access.
Flask
def edit_article(): if current_user.role [1] 'editor': return 'Edit page' else: return [2](403)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '!=' instead of '=='
Using redirect instead of abort
✗ Incorrect
Use '==' to compare role and abort(403) to deny access.
5fill in blank
hardFill all three blanks to create a dictionary comprehension filtering users with role 'admin'.
Flask
admins = {user['[1]']: user for user in users if user['[2]'] [3] 'admin'} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using email as key
Using '!=' instead of '=='
✗ Incorrect
The dictionary keys are usernames, filtered by role equal to 'admin'.