0
0
Flaskframework~10 mins

Template-level authorization 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 check if the user is authenticated in the Flask template.

Flask
{% if [1] %}
  <p>Welcome, user!</p>
{% endif %}
Drag options to blanks, or click blank then click option'
Acurrent_user.is_authenticated
Buser.is_authenticated
Cauth.is_authenticated
Dsession.is_authenticated
Attempts:
3 left
💡 Hint
Common Mistakes
Using user.is_authenticated which is undefined in the template.
Trying to access session.is_authenticated which does not exist.
2fill in blank
medium

Complete the code to show a logout link only if the user is authenticated.

Flask
{% if [1] %}
  <a href="/logout">Logout</a>
{% endif %}
Drag options to blanks, or click blank then click option'
Asession.logged_in
Bauth.is_authenticated
Cuser.logged_in
Dcurrent_user.is_authenticated
Attempts:
3 left
💡 Hint
Common Mistakes
Using session.logged_in which is not standard in Flask-Login.
Using undefined variables like user.logged_in.
3fill in blank
hard

Fix the error in the template code to correctly check if the user has the 'admin' role.

Flask
{% if [1] %}
  <p>Admin Panel</p>
{% endif %}
Drag options to blanks, or click blank then click option'
Acurrent_user.roles.contains('admin')
B'admin' in current_user.roles
C'admin' == current_user.roles
Dcurrent_user.role == 'admin'
Attempts:
3 left
💡 Hint
Common Mistakes
Using equality operator to compare string to list.
Using a method contains which does not exist in Python lists.
4fill in blank
hard

Fill both blanks to display a message only if the user is authenticated and has the 'editor' role.

Flask
{% if [1] and [2] %}
  <p>Editor Access Granted</p>
{% endif %}
Drag options to blanks, or click blank then click option'
Acurrent_user.is_authenticated
B'editor' in current_user.roles
Ccurrent_user.role == 'editor'
Duser.is_authenticated
Attempts:
3 left
💡 Hint
Common Mistakes
Using current_user.role == 'editor' which assumes a single role string.
Using undefined variable user.is_authenticated.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that maps usernames to emails only for authenticated users with verified emails.

Flask
{% raw %}{% set user_emails = { [1]: [2] for [3] in users if current_user.is_authenticated and user.verified } %}{% endraw %}
Drag options to blanks, or click blank then click option'
Auser.username
Buser.email
Cuser
Dusername
Attempts:
3 left
💡 Hint
Common Mistakes
Using undefined variable username as loop variable.
Swapping key and value in the dictionary comprehension.