from functools import wraps from django.utils.decorators import classonlymethod def view_decorator(fdec, subclass=False): """ Change a function decorator into a view decorator. https://github.com/lqc/django/tree/cbvdecoration_ticket14512 """ def decorator(cls): if not hasattr(cls, "as_view"): raise TypeError("You should only decorate subclasses of View, not mixins.") if subclass: cls = type("%sWithDecorator(%s)" % (cls.__name__, fdec.__name__), (cls,), {}) original = cls.as_view.im_func @wraps(original) def as_view(current, **initkwargs): return fdec(original(current, **initkwargs)) cls.as_view = classonlymethod(as_view) return cls return decorator