The login_required Decorator
In this 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.
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.
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.
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. #}
- Forgetting to set LOGIN_URL in settings.py, so Django can't find where to redirect anonymous users.
- Applying login_required to a class-based view directly instead of using LoginRequiredMixin.
- Manually writing 'if not request.user.is_authenticated: redirect(...)' in every view instead of using the decorator.
- @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.
Chapter Quiz — Complete all 12 topics to unlock
0/12 topics done
Complete these topics first:
- Introduction to Django's Auth System
- The User Model Overview
- Setting Up a Login View
- Logout Functionality
- User Registration Form
- The login_required Decorator
- The permission_required Decorator
- Django Groups and Permissions
- Password Hashing in Django
- Session Authentication Basics
- Introduction to Custom User Models
- Sending Emails with Django