Definition
Authorization in Django is the process of verifying what an authenticated user is allowed to do (permissions, roles, and ownership).
Why It Matters
Authentication says “I am Alice”; Authorization says “Alice is allowed to edit this specific post but not that one.” Without robust authorization, even a secure login system remains vulnerable to data leaks and unauthorized manipulation.
Core Concepts
- Permissions: Flags assigned to users or groups (e.g.,
can_add_log). - Decorators:
@login_requiredensures a user is logged in. - Ownership Checks: Logical checks in views to ensure a user can only access their own data.
from django.contrib.auth.decorators import login_required
from django.http import Http404
@login_required
def edit_entry(request, entry_id):
"""Edit an existing entry."""
entry = Entry.objects.get(id=entry_id)
# Authorization check: Ensure entry belongs to user
if entry.topic.owner != request.user:
raise Http404
# ... logic to edit entry
Connected Concepts
- Django Authentication
- Django Web Framework (MVT)
- agency-theory