Skip to content

Instantly share code, notes, and snippets.

@dimkoug
Last active June 22, 2017 05:40
Show Gist options
  • Save dimkoug/2019aecfb63c7e08279597668d1c035c to your computer and use it in GitHub Desktop.
Save dimkoug/2019aecfb63c7e08279597668d1c035c to your computer and use it in GitHub Desktop.
Login-Protect Class-Based Views Django Framework
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