@permission_required('app.view_item'). What is the typical behavior if a logged-in user lacks this permission?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')
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.
The @permission_required decorator accepts a single permission string. To require multiple permissions, stack multiple decorators.
from django.contrib.auth.decorators import permission_required from django.http import HttpResponse @permission_required def my_view(request): return HttpResponse('Deleted')
The decorator must be called with parentheses and the permission string as an argument. Missing parentheses means the decorator is not applied.
@permission_required, what HTTP status code does Django return?Django returns a 403 Forbidden status code when an authenticated user lacks the required permission.
@permission_required redirects unauthorized users to the login page. How can you change this redirect URL to a custom page?The @permission_required decorator accepts a login_url argument to specify a custom redirect URL for unauthorized users.
