Skip to content

Instantly share code, notes, and snippets.

@rochacon
Created October 5, 2011 08:21
Show Gist options
  • Save rochacon/1263923 to your computer and use it in GitHub Desktop.
Save rochacon/1263923 to your computer and use it in GitHub Desktop.
Django LoginRequiredView, for easy protected class based views
"""
This is a simple class based view that require the user login to be displayed
It's is intented to be used as a base class for another class based views
Use it like this:
from django.views.generic import TemplateView
from mylib import LoginRequiredView
class LoginRequiredPage(LoginRequiredView, TemplateView):
template_name = 'login_required_page.html'
"""
from django.contrib.auth.decorators import login_required
from django.utils.decorators import method_decorator
from django.views.generic import View
class LoginRequiredView(View):
@method_decorator(login_required)
def dispatch(self, request, *args, **kwargs):
return super(LoginRequiredView, self).dispatch(request, *args, **kwargs)
@emyller
Copy link

emyller commented Aug 30, 2017

Only recently Django has implemented its own LoginRequiredMixin. Nice snippet though -- has been useful for years!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment