0
0
Flaskframework~20 mins

Template-level authorization in Flask - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Template Authorization Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What will this Flask template render when user is not admin?

Consider this Flask Jinja2 template snippet:

{% if current_user.is_admin %}
  
{% else %}
  

You do not have permission to delete posts.

{% endif %}

If current_user.is_admin is False, what will be rendered?

A<p>You do not have permission to delete posts.</p>
B<button>Delete Post</button>
CNothing will be rendered
DAn error will occur because current_user is undefined
Attempts:
2 left
💡 Hint

Think about how the {% if %} condition controls what is shown.

📝 Syntax
intermediate
2:00remaining
Identify the syntax error in this Jinja2 authorization check

Which option contains a syntax error in this template authorization snippet?

{% if user.is_authenticated and user.is_admin %}
  

Welcome, admin!

{% endif %}
A
{% if user.is_authenticated &amp;&amp; user.is_admin %}
  &lt;p&gt;Welcome, admin!&lt;/p&gt;
{% endif %}
B
{% if user.is_authenticated and user.is_admin %}
  &lt;p&gt;Welcome, admin!&lt;/p&gt;
{% endif %}
C
{% if user.is_authenticated and user.is_admin %}
  &lt;p&gt;Welcome, admin!&lt;/p&gt;
{% end %}
D
{% if user.is_authenticated and user.is_admin %}
  &lt;p&gt;Welcome, admin!&lt;/p&gt;
Attempts:
2 left
💡 Hint

Check the logical operator syntax in Jinja2.

state_output
advanced
2:00remaining
What is the output when user is authenticated but not admin?

Given this Flask template snippet:

{% if user.is_authenticated %}
  {% if user.is_admin %}
    

Admin Panel

{% else %}

User Dashboard

{% endif %} {% else %}

Please log in.

{% endif %}

If user.is_authenticated is True and user.is_admin is False, what will be rendered?

A<p>Admin Panel</p>
BNothing will be rendered
C<p>Please log in.</p>
D<p>User Dashboard</p>
Attempts:
2 left
💡 Hint

Follow the nested {% if %} conditions carefully.

🔧 Debug
advanced
2:00remaining
Why does this template raise an error when user is None?

Consider this template snippet:

{% if user.is_admin %}
  

Admin content

{% else %}

Regular content

{% endif %}

If user is None, what error occurs and why?

ATypeError because user is not a boolean
BSyntaxError due to missing endif
CAttributeError because NoneType has no attribute 'is_admin'
DNo error, renders 'Regular content'
Attempts:
2 left
💡 Hint

Think about what happens when you try to access a property on None.

🧠 Conceptual
expert
3:00remaining
Which template-level authorization approach prevents unauthorized content rendering best?

In Flask templates, which approach best ensures unauthorized users cannot see admin-only content?

ARender all content and rely on CSS to hide admin sections from unauthorized users
BUse <code>{% if current_user.is_admin %}</code> to conditionally show admin content in templates
CSend all data to the template and use JavaScript to hide admin content on the client side
DDo not check authorization in templates; only check in backend routes
Attempts:
2 left
💡 Hint

Think about where content is actually removed versus just hidden.