How to use login_not_required with class-based views
Django 5.1 introduced the LoginRequiredMiddleware. This comes with the companion decorator login_not_required() for function-based views that don't need authentication. What do I do for a class-based view that doesn't need authentication?
Django provides the method_decorator for decorating functions class-based views. In this case, you are trying to modify the dispatch method of the class, as follows:
from django.contrib.auth.decorators import login_not_required
from django.utils.decorators import method_decorator
from django.views.generic import View
@method_decorator(login_not_required, name="dispatch")
class MyView(View):
# ...