Created
October 5, 2011 08:21
-
-
Save rochacon/1263923 to your computer and use it in GitHub Desktop.
Django LoginRequiredView, for easy protected class based views
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Only recently Django has implemented its own
LoginRequiredMixin
. Nice snippet though -- has been useful for years!