StaffCoder
Django
DjangoDjango 5.2Python 3.13
18 modules · 72 challengesSign up
Module 9 · Authentication & Authorization

Session Access Flow

mediumDjango AuthenticationAccess Control100%2 of 2 solved

Problem statement

In this Django project, implement signing in, signing out, and the gate on the member pages. Wire Django's own LoginView and LogoutView in app/urls.py, fill in app/templates/registration/login.html, and gate dashboard and invoices in app/views.py. This closes a site whose protected pages are protected by nothing and that has no way to sign in at all.

Requirements

Implement:

  • the login and logout routes in app/urls.py
  • the gate on dashboard and invoices in app/views.py
  • the form in app/templates/registration/login.html

The following files are already implemented and provided: app/views.py (the three page views, ungated), app/urls.py (the three page routes), project/settings.py and the dashboard and invoices templates. Users are the standard django.contrib.auth.models.User, and django.contrib.auth, the session app and their middleware are installed. The dashboard template already carries a POST form aimed at /accounts/logout/ and a link to the invoices page. app/templates/registration/login.html exists as an empty shell.

Of the three settings that govern this flow, only LOGIN_URL is set, as /accounts/login/. LOGIN_REDIRECT_URL is Django's default /accounts/profile/, which is not a route in this project, and LOGOUT_REDIRECT_URL is Django's default None. project/settings.py is read-only, so set both landings per route with next_page, which is the per-route version of those settings, takes precedence over them, and accepts a route name as happily as a path.

1. GET /accounts/login/

  • Purpose: Display the sign-in form.
  • Authentication: Not required.
  • Response:
    • 200 OK: a POST form carrying the form's username and password fields. LoginView renders registration/login.html with no configuration and puts an AuthenticationForm in the context as form, so the template only lays it out.

2. POST /accounts/login/

  • Purpose: Sign a visitor in.
  • Authentication: Not required.
  • Response:
    • 302 Found: to /members/ when the credentials are correct and the request carries no return address, with a session established.
    • 302 Found: to the page named in next when the request carries one, rather than to the default landing page.
    • 200 OK: the login page again, with no session established and this form-wide error printed on it:
Please enter a correct username and password. Note that both fields may be case-sensitive.

3. GET /members/ and GET /members/invoices/

  • Purpose: The member dashboard and the member invoice page.
  • Authentication: Required.
  • Response:
    • 200 OK: the page, for a signed-in visitor.
    • 302 Found: to /accounts/login/?next= plus the page that was asked for, for an anonymous visitor.

4. POST /accounts/logout/

  • Purpose: Sign a visitor out.
  • Response:
    • 302 Found: to the public page at /, with the session ended.
  • A GET of the same route leaves the session alone. LogoutView accepts POST and OPTIONS only, so a link or a prefetch cannot end somebody's session.

Notes

  • A rejected sign-in is not a redirect: the view re-renders at 200 with a bound, invalid form, so a template that prints fields without printing errors shows the visitor nothing at all. {{ form.as_p }} does both.
  • A form with method="post" and no action posts to the current URL including its query string, which is how next survives the round trip. A hidden next input works too, since LoginView puts the value in the context.
  • Reach for the built-in LoginView and LogoutView rather than hand-rolling a pair around authenticate(), login() and logout().
  • Do not reach for permissions, groups or a custom user model: this is the sign-in flow on the default User.
  • Keep {% csrf_token %} in both POST forms: a browser's POST without it is refused.

More in Authentication & Authorization

See the module
Permission Gated PublishingPublishing and unpublishing are gated on being signed in and nothing more, so every author can change the state of anyone's article. Gate both actions on the model permission app.publish_article, hide the controls from a visitor who does not hold it, and let the editors group grant it.100%2 of 2 solvedmediumEmail Login Custom UserThe product signs users up by email, but the shipped user model still identifies people by a username. Replace it with a custom user model that authenticates by email, backed by a manager that creates users and superusers correctly.100%2 of 2 solvedmediumMembers Area Access MatrixThe members area has a public page, member pages, permission-gated actions and a staff-only section, and today every one of them is reachable by everyone. Enforce the whole access matrix across the view and template layers, with a login redirect for anonymous visitors and a 403 for signed-in visitors who are not allowed.100%2 of 2 solvedmedium

We use cookies that are necessary to run Staffcoder, and optional analytics cookies to see which pages get used. Analytics stays off unless you accept. Read our Cookie Policy.