liveBook · Manning

Authorization

As explained in Django in Action

Authorization is the process of determining which actions or resources an authenticated user is permitted to access.

Access control decides what an authenticated user may do. Authentication proves identity; authorization applies permissions to that identity. On a multiuser site, this keeps readers from editing articles and prevents rival bands from changing one another’s profiles. section 1.1.4 section 6.1

In Django, begin by requiring a login, then add view-specific checks when access depends on ownership or relationships. Session middleware determines whether a user is logged in before the view runs; the request then exposes the authenticated user to the view. section 6.1 section 6.4

A practical access-control flow is:

  • Wrap a protected view with @login_required so anonymous visitors are redirected to a login page. section 6.3
  • Resolve the requested object, such as a Musician, with get_object_or_404(). section 6.4
  • Compare request.user’s profile relationships with the requested object or its band membership. section 6.4
  • Render the permitted response, or raise an error when the user is not allowed. section 6.4

Restricting a musician profile

A musician-profile view first requires login. It then permits access when the current user owns that musician profile or shares a band with that musician. A user who is neither the musician nor a bandmate receives a 404 response. This separates the broad “is logged in” check from the narrower “may view this profile” rule. section 6.4 section 6.3

@login_required is intentionally simple: any active authenticated account may proceed. More detailed permissions belong in the relevant view, where the rule can use the requested object and the current user’s related data. Django’s User can be extended through a profile linked by OneToOneField, or replaced with a custom user model. section 6.3 section 6.8

Figure 6.4 Authentication verification steps for visiting a @login_required wrapped view
figure

Follow the request path to see where Django verifies login status before allowing a @login_required view to run. section 6.3

How Django in Action covers it

How deep it goes
Demonstrates decorator-based login restriction and relationship checks inside a view, while deferring custom middleware.
Examples
Uses protected generic pages and musician profiles accessible only to owners or bandmates.
What it assumes
Introduces authentication and authorization from basic multiuser-site needs without requiring prior HTTP or database knowledge.
What you can do after
Apply it via Django’s @login_required decorator and custom view logic for ownership or membership rules.
How it connects
Connects authentication, sessions, middleware, Django views, User profiles, ORM relationships, and the Django Admin.

Also explained in

Compare how 28 books explain authorization →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Django in Action. The section links point to the parts of the book it draws on.