← Back to Django Course | Chapter 10: Authentication & Authorization | Lesson 10 of 12

Session Authentication Basics

A session is like a wristband stamped at the door — once you're stamped, Django remembers you're allowed in without asking for your password on every page.

How Sessions Work

When a user logs in, Django creates a session record and sends the browser a sessionid cookie. Every later request uses that cookie to look up the session and find the logged-in user.

Example: How Sessions Work

request.session.session_key is the same value stored in the visitor's sessionid cookie, linking the browser to server-side session data.

markup
def whoami(request):
    print(request.session.session_key)
    print(request.user)
    return render(request, 'home.html')
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}

Storing Custom Data in a Session

Beyond login state, request.session behaves like a dictionary you can use to remember small pieces of per-visitor data.

Warning: Don't store large or sensitive data directly in the session — keep it small.

Example: Storing Custom Data in a Session

Anything assigned to request.session is automatically saved and available again on the visitor's next request.

markup
def set_theme(request):
    request.session['theme'] = 'dark'
    return redirect('home')
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}

Session Expiry Settings

SESSION_COOKIE_AGE controls how many seconds a session cookie lasts before it expires.

Example: Session Expiry Settings

Without this setting, Django's default session cookie expires as soon as the browser is closed.

markup
SESSION_COOKIE_AGE = 1209600  # 2 weeks, in seconds
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}
Common Mistakes
  1. Assuming sessions persist forever — by default Django's session cookie expires when the browser closes unless SESSION_COOKIE_AGE is configured.
  2. Manually reading cookies to check login state instead of relying on request.user, which Django already resolves from the session.
  3. Forgetting that logging out on one device doesn't automatically end sessions on other devices unless sessions are explicitly cleared.
Chapter Summary
  • Django's session framework stores a per-visitor session ID in a cookie.
  • The actual session data lives server-side (in the database by default), keyed by that session ID.
  • login() writes the authenticated user's ID into the session; every later request reads it back via AuthenticationMiddleware.
  • Sessions are what let request.user stay populated across multiple page loads.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.