Skip to content

Instantly share code, notes, and snippets.

@ojii
Created October 7, 2011 10:09
Show Gist options
  • Save ojii/1269957 to your computer and use it in GitHub Desktop.
Save ojii/1269957 to your computer and use it in GitHub Desktop.
Django view decorator to CBV decorator
from django.utils.decorators import method_decorator
def cbv_decorator(decorator):
"""
Turns a normal view decorator into a class-based-view decorator.
Usage:
@cbv_decorator(login_required)
class MyClassBasedView(View):
pass
"""
def _decorator(cls):
cls.dispatch = method_decorator(decorator)(cls.dispatch)
return cls
return _decorator
@GeoffMahugu
Copy link

Thank you

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