Generic View Mixins
In this page:
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.
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.
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.
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. #}
- Getting the mixin order wrong in the class definition — mixins must come before the base generic view in the parentheses.
- Forgetting that LoginRequiredMixin needs login_url or a default LOGIN_URL setting to know where to redirect anonymous users.
- Combining mixins that both define the same method (like get_queryset) without checking which one actually wins through MRO.
- 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.
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: