Django·Django from project to API

Authentication

Use Django’s auth views, sessions and permission checks.

Intermediate 13 min read Lesson 7 of 300 Challenge included
What you will be able to do
  • Use the built-in LoginView and LogoutView.
  • Protect views with login_required.
  • Check object permissions, not just login state.
  • Never store plaintext passwords.

In plain English

Django ships authentication: user model, password hashing, sessions and login views. Use the built-in views first and add permission checks with login_required or LoginRequiredMixin. Do not build password storage yourself.

views.pyPython
from django.contrib.auth.decorators import login_required
from django.shortcuts import render

@login_required
def dashboard(request):
    return render(request, 'dashboard.html')
Your turn

Protect a view

Easy

Add login_required to a view function named dashboard.

PYTHON · starter
def dashboard(request):
Check your understanding before moving on.
Knowledge checkQuestion 1

Which decorator requires a signed-in user for a view?

Select an answer to continue
Study tools

Make it yours.

Keep a private note for this lesson, then turn the key ideas into flashcards you can revisit.

Sign in to save notes and flashcards.Both are private to your account and stay with your progress.
Sign in
Finished reading?

Lock in what you just learned.

Mark this lesson complete to save your place and move one step closer to the certificate.

Next: Querysets
Lesson 7 of 300 · 0% of course