1👍
✅
Yes, just use the login_required
decorator or LoginRequiredMixin
from django.contrib.auth.decorators import login_required
@login_required
def dashboard(request):
return render(request, 'home/dashboard.py')
from django.contrib.auth.mixins import LoginRequiredMixin
class MyCBV(LoginRequiredMixin, GenericView):
What this will do is redirect anyone attempting to access the view back to the LOGIN_URL
(which can be overridden here) with a next get parameter back to the view, so that they must login before continuing. This isn’t the same as what you currently do, but its much friendlier
If your entire website needs to be logged in, then you can use a middleware to make this the default
Source:stackexchange.com