0Pricing
Django Academy · Lesson

login, logout, and Auth Views

Wire up Django's ready-made auth views.

login, logout, and Auth Views is a free Django Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Django Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

From Verify to Logged In

Verifying a user is only step one. To keep them logged in across requests, you start a session with Django's login helper. 🔑

The login() Function

login() takes the request and an authenticated user, then saves their id in the session. Future requests now know who they are.

from django.contrib.auth import login

Using login() in a View

After authenticate() succeeds, call login(request, user). From that point on, request.user is the signed-in person, not a guest.

if user is not None:
    login(request, user)

The logout() Function

To end a session, call logout(request). It clears the stored user id and any session data tied to that visit. 👋

from django.contrib.auth import logout
logout(request)

request.user Everywhere

Once logged in, request.user is available in every view. Check user.is_authenticated to tell members apart from anonymous visitors.

if request.user.is_authenticated:
    name = request.user.username

Skip the Boilerplate

Django ships ready-made auth views so you do not hand-write login pages. They handle the form, validation, and session for you.

Wire Up Auth URLs

Include django.contrib.auth.urls to get login, logout, and password routes instantly under a path you choose.

path("accounts/", include("django.contrib.auth.urls"))

LoginView in Action

The built-in LoginView renders a form and logs the user in on success. You mainly supply a template named registration/login.html.

LoginView Template Context

LoginView passes a form variable to your template. Just render that form inside a POST and Django handles the rest.

Where to Go After Login

Set LOGIN_REDIRECT_URL in settings to control where users land after a successful login, such as a dashboard.

LOGIN_REDIRECT_URL = "/dashboard/"

Logout Redirects Too

The matching LogoutView ends the session, and LOGOUT_REDIRECT_URL decides where signed-out users are sent next.

Quick Check

Which function actually starts a logged-in session?

Recap: Sessions and Auth Views

You used login() and logout() to manage sessions, and wired Django's auth views and redirect settings to avoid boilerplate. ✅

Frequently asked questions

Is the “login, logout, and Auth Views” lesson free?

Yes — the full text of “login, logout, and Auth Views” is free to read here on the web, and the Django Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Django Academy course, upgrade to CoddyKit PRO.

What will I learn in “login, logout, and Auth Views”?

Wire up Django's ready-made auth views. You practise Django Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Django Academy?

No prior experience is required. Django Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “login, logout, and Auth Views” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Django Academy lesson?

Yes. Every Django Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. The User Model and authenticate()
  2. login, logout, and Auth Views
  3. Registration with UserCreationForm
  4. login_required and Protecting Views
← Back to Django Academy