About Decorators for Class-based Views

To apply view decorators (like @never_cache or @login_required) to a django class-based view, you need to wrap them with @method_decorator:

1
2
3
4
5
6
7
from django.contrib.auth.decorators import login_required
from django.utils.decorators import method_decorator
from django.views.generic import TemplateView

@method_decorator(login_required, name="dispatch")
class DashboardView(TemplateView):
    template_name = "accounts/dashboard.html"

Tips and Tricks Programming Django 4.2 Django 3.2 Django 2.2