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.
