← Back to Django Course | Chapter 9: Class-Based Views | Lesson 8 of 10

Generic View Mixins

A mixin is a small add-on class that hands a class-based view extra powers, like giving a plain car a GPS without rebuilding the whole car.

What a Mixin Adds to a View

A mixin is just a Python class meant to be combined with another class through multiple inheritance. Django's generic-view mixins add checks (like "is this user logged in?") that run before the view's normal get()/post() logic executes.

Example: What a Mixin Adds to a View

LoginRequiredMixin runs its check before ListView's own logic, blocking anonymous visitors from ever reaching the list.

markup
from django.contrib.auth.mixins import LoginRequiredMixin
from django.views.generic import ListView
from .models import Book


class BookListView(LoginRequiredMixin, ListView):
    model = Book
{# 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. #}

Requiring Login with LoginRequiredMixin

LoginRequiredMixin redirects anonymous users to the login page (defined by LOGIN_URL in settings, or login_url on the mixin) before the view logic runs at all.

Example: Requiring Login with LoginRequiredMixin

An anonymous request to BookDetailView is redirected to /accounts/login/ before DetailView ever fetches the object.

markup
from django.contrib.auth.mixins import LoginRequiredMixin
from django.views.generic import DetailView
from .models import Book


class BookDetailView(LoginRequiredMixin, DetailView):
    model = Book
    login_url = '/accounts/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. #}

Requiring a Permission with PermissionRequiredMixin

PermissionRequiredMixin checks that the logged-in user has a specific permission string before allowing access, returning a 403 Forbidden response if they do not.

Example: Requiring a Permission with PermissionRequiredMixin

Only users holding the "library.delete_book" permission can reach BookDeleteView; everyone else gets a 403.

markup
from django.contrib.auth.mixins import PermissionRequiredMixin
from django.views.generic.edit import DeleteView
from .models import Book


class BookDeleteView(PermissionRequiredMixin, DeleteView):
    model = Book
    permission_required = 'library.delete_book'
{# 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. Getting the mixin order wrong in the class definition — mixins must come before the base generic view in the parentheses.
  2. Forgetting that LoginRequiredMixin needs login_url or a default LOGIN_URL setting to know where to redirect anonymous users.
  3. Combining mixins that both define the same method (like get_queryset) without checking which one actually wins through MRO.
Chapter Summary
  • A mixin is a small reusable class that adds behavior to a class-based view through multiple inheritance.
  • LoginRequiredMixin blocks anonymous users and redirects them to the login page.
  • PermissionRequiredMixin blocks users lacking a specific permission.
  • Mixins are listed before the base generic view class, since Python resolves methods left to right.

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.