Challenge - 5 Problems
Permission Decorator Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What happens when a user without permission accesses a view with @permission_required?
Consider a Django view decorated with
@permission_required('app.view_item'). What is the typical behavior if a logged-in user lacks this permission?Django
from django.contrib.auth.decorators import permission_required from django.http import HttpResponse @permission_required('app.view_item') def my_view(request): return HttpResponse('Access granted')
Attempts:
2 left
💡 Hint
Think about how Django handles permissions differently for authenticated and anonymous users.
✗ Incorrect
The @permission_required decorator first checks if the user is authenticated. If not, it redirects to the login page. If authenticated but lacking permission, it returns a 403 Forbidden response.
📝 Syntax
intermediate2:00remaining
Identify the correct syntax to require multiple permissions with @permission_required
Which of the following correctly requires both 'app.view_item' and 'app.change_item' permissions on a Django view?
Attempts:
2 left
💡 Hint
Think about stacking decorators to require multiple permissions.
✗ Incorrect
The @permission_required decorator accepts a single permission string. To require multiple permissions, stack multiple decorators.
🔧 Debug
advanced2:00remaining
Why does this @permission_required decorator not work as expected?
This view should require 'app.delete_item' permission, but users without permission can access it. What is the problem?
Django
from django.contrib.auth.decorators import permission_required from django.http import HttpResponse @permission_required def my_view(request): return HttpResponse('Deleted')
Attempts:
2 left
💡 Hint
Check how decorators are applied in Python syntax.
✗ Incorrect
The decorator must be called with parentheses and the permission string as an argument. Missing parentheses means the decorator is not applied.
❓ state_output
advanced2:00remaining
What is the HTTP status code returned when permission is denied by @permission_required?
When a logged-in user lacks the required permission for a view decorated with
@permission_required, what HTTP status code does Django return?Attempts:
2 left
💡 Hint
Think about the meaning of HTTP status codes for permission issues.
✗ Incorrect
Django returns a 403 Forbidden status code when an authenticated user lacks the required permission.
🧠 Conceptual
expert3:00remaining
How to customize the redirect URL for unauthorized users with @permission_required?
By default,
@permission_required redirects unauthorized users to the login page. How can you change this redirect URL to a custom page?Attempts:
2 left
💡 Hint
Check the parameters accepted by the decorator.
✗ Incorrect
The @permission_required decorator accepts a login_url argument to specify a custom redirect URL for unauthorized users.