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

The login_required Decorator

The login_required decorator is a locked door sign — it sends anyone who isn't logged in straight to the login page.

Protecting a View

@login_required checks request.user.is_authenticated before running the view body, redirecting anonymous users instead.

Example: Protecting a View

If the visitor isn't logged in, Django redirects them to LOGIN_URL before dashboard() ever runs.

markup
from django.contrib.auth.decorators import login_required
from django.shortcuts import render

@login_required
def dashboard(request):
    return render(request, 'dashboard.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. #}

Setting LOGIN_URL

LOGIN_URL tells Django where to send anonymous users who hit a protected view.

Warning: Without LOGIN_URL, Django defaults to /accounts/login/, which may not exist in your project.

Example: Setting LOGIN_URL

Setting this to the URL name login means Django resolves it to wherever that named URL actually points.

markup
LOGIN_URL = 'login'
{# 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. #}

Returning After Login

Django appends ?next=/dashboard/ to the login redirect so the login view can send the user back afterward.

Example: Returning After Login

Reading next from the submitted form sends the user back to the page they originally tried to visit.

markup
from django.contrib.auth import authenticate, login
from django.shortcuts import redirect, render

def login_view(request):
    if request.method == 'POST':
        user = authenticate(
            request, username=request.POST['username'],
            password=request.POST['password'],
        )
        if user:
            login(request, user)
            return redirect(request.POST.get('next', 'home'))
    return render(request, 'login.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. #}
Common Mistakes
  1. Forgetting to set LOGIN_URL in settings.py, so Django can't find where to redirect anonymous users.
  2. Applying login_required to a class-based view directly instead of using LoginRequiredMixin.
  3. Manually writing 'if not request.user.is_authenticated: redirect(...)' in every view instead of using the decorator.
Chapter Summary
  • @login_required wraps a view so only authenticated users can access it.
  • Anonymous visitors are redirected to LOGIN_URL, with a ?next= parameter to return them after login.
  • It applies to function-based views; class-based views use LoginRequiredMixin instead.

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.