Notes

Source: 📖 Django for Professionals ch13 p185


Implementing login required behaviour

There are two easy ways of implementing login required behaviour when a view is called — either with the @login_required decorator if using a function-based view, or with the LoginRequiredMixin if using a class-based view. Below we implement it using the mixin.


# views.py

from django.contrib.auth.mixins import LoginRequiredMixin


class MyView(LoginRequiredMixin):
	...
	login_url = 'account_login'

We first import LoginRequiredMixin, then we make our view inherit from it. The last step is to provide login_url, the name of the URL where we want the site to redirect the user to if they are not logged in when calling this view.

We use account_login assuming that we have used django-allauth for our authentication system, otherwise it would simply be login if we were using Django's built in auth.


See also: