Last active
June 22, 2017 05:40
-
-
Save dimkoug/2019aecfb63c7e08279597668d1c035c to your computer and use it in GitHub Desktop.
Login-Protect Class-Based Views Django Framework
This file contains 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
from django.contrib.auth.decorators import login_required | |
from django.utils.decorators import method_decorator | |
from django.views.generic import ListView, DetailView | |
from django.views.generic.edit import CreateView, UpdateView, DeleteView | |
@method_decorator(login_required, name='dispatch') | |
class ViewMixin(object): | |
def dispatch(self, *args, **kwargs): | |
for key, value in self.kwargs.items(): | |
setattr(self, key, value) | |
return super(ViewMixin, self).dispatch(*args, **kwargs) | |
class ProtectedListView(ViewMixin, ListView): | |
pass | |
class ProtectedDetailView(ViewMixin, DetailView): | |
pass | |
class ProtectedCreateView(ViewMixin, CreateView): | |
pass | |
class ProtectedUpdateView(ViewMixin, UpdateView): | |
pass | |
class ProtectedDeleteView(ViewMixin, DeleteView): | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment