0
0
Djangoframework~20 mins

Template permission checks in Django - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Template Permission Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
1:30remaining
What will this Django template render?
Given the following Django template snippet, what will be rendered if the user has the 'app.view_item' permission?

{% if perms.app.view_item %}You can view this item.{% else %}Access denied.{% endif %}
Django
{% if perms.app.view_item %}You can view this item.{% else %}Access denied.{% endif %}
AYou can view this item.
BAccess denied.
CAn error occurs because 'perms' is undefined.
DNothing is rendered.
Attempts:
2 left
💡 Hint
The 'perms' variable in Django templates checks user permissions.
📝 Syntax
intermediate
1:30remaining
Identify the syntax error in this permission check
What is wrong with this Django template code?

{% if perms.app.view_item == True %}Allowed{% else %}Denied{% endif %}
Django
{% if perms.app.view_item == True %}Allowed{% else %}Denied{% endif %}
AThe comparison '== True' is unnecessary and causes a syntax error.
BThe 'perms' variable cannot be used in templates.
CThe 'else' tag is missing a colon.
DThe template tag should be {% if perms.app.view_item %} without '== True'.
Attempts:
2 left
💡 Hint
Django template if tags do not require explicit boolean comparisons.
🔧 Debug
advanced
2:00remaining
Why does this permission check always fail?
Consider this Django template snippet:

{% if perms.app.view_item %}Show{% else %}Hide{% endif %}

The user has the permission 'app.view_item', but the template always renders 'Hide'. Why?
Django
{% if perms.app.view_item %}Show{% else %}Hide{% endif %}
AThe user object is not passed to the template context, so 'perms' is empty.
BThe permission name is incorrect; it should be 'app.viewitems'.
CThe template tag syntax is invalid and causes the condition to always be false.
DThe user is anonymous, so no permissions are granted.
Attempts:
2 left
💡 Hint
Check if the user context is available in the template.
state_output
advanced
2:00remaining
What is the output of this permission loop?
Given this Django template code:

{% for perm in perms.app %}{{ perm }} {% endfor %}

What will be the output if the user has permissions 'app.view_item' and 'app.change_item'?
Django
{% for perm in perms.app %}{{ perm }} {% endfor %}
AOutputs 'view_item change_item '
BOutputs nothing because 'perms.app' is empty.
CRaises an error because 'perms.app' is not iterable.
DOutputs 'app.view_item app.change_item '
Attempts:
2 left
💡 Hint
'perms.app' is not a list but a permission checker object.
🧠 Conceptual
expert
2:30remaining
Which template code correctly checks multiple permissions?
You want to show a message only if the user has both 'app.view_item' and 'app.change_item' permissions. Which Django template snippet does this correctly?
A{% if perms.app.view_item %}{% if perms.app.change_item %}Allowed{% else %}Denied{% endif %}{% else %}Denied{% endif %}
B{% if perms.app.view_item and perms.app.change_item %}Allowed{% else %}Denied{% endif %}
C{% if perms.app.view_item or perms.app.change_item %}Allowed{% else %}Denied{% endif %}
D{% if perms.app.view_item & perms.app.change_item %}Allowed{% else %}Denied{% endif %}
Attempts:
2 left
💡 Hint
Use 'and' to combine conditions in Django templates.