Skip to main content

A collection of decorator mixins for Django views.

Project description

A collection of class-based view mixins for Django. Django-Decoratormixins also includes a function for converting view decorators into class-based view mixins.


If you have a decorator, usage is as simple as calling DecoratorMixin and passing the decorator as the argument.

from decoratormixins import DecoratorMixin

def my_decorator(f):
    # ...

MyDecMixin = DecoratorMixin(my_decorator)

Decorator Factories

Many of Django’s built in decorators are actually decorator factories. In this case, it is necessary to get a decorator out of them before it is possible to create a mixin from it.

Here we have a decorator factory that modifies a view to require a certain parameter in a get request:

def require_get_param(param):
    def actual_decorator(view_func):
        def _wrapped_view_func(request, *args, **kwargs):
            if request.GET.get(param, None) is not None:
                response = view_func(request, *args, **kwargs)
                response.content += param
                return response
                return HttpResponseNotFound("{} is not present.".format(param))

        return _wrapped_view_func
    return actual_decorator

In order to use it, we must specify the parameter we are requiring:

require_foo_decorator = require_get_param('foo')

And now we can call DecoratorMixin on that decorator:

RequireFooMixin = DecoratorMixin(require_foo_decorator)

Applying Mixins

Once you have the mixin you reqire, mix it in with a class-based view.

class TestView(View):
    def get(self, request):
        return HttpResponse("OK")

class TestViewFoo(RequireFooMixin, TestView):

TestViewFoo.as_view() will return a view method that is usable in your

Composing Mixins

It is possible to use multiple mixins for a single class, but order matters. The leftmost mixin in a class definition will be the outermost decorator.

from decoratormixins.auth import LoginRequiredMixin
from decoratormixins.http import RequireGetMixin

# TestView from above

class LoggedInGetRequestView(LoginRequiredMixin,

Included Mixins

Here is a list of all of the included mixins, and the modules in which they can be found.

  • decoratormixins.auth

    • LoginRequiredMixin

  • decoratormixins.csrf

    • CsrfProtectMixin

    • EnsureCsrfCookieMixin

    • CsrfExemptMixin

  • decoratormixins.http

    • ConditionalPageMixin

    • RequireGetMixin

    • RequirePostMixin

    • RequireSafeMixin

    • EtagMixin

    • LastModifiedMixin

  • decoratormixins.cache

    • NeverCacheMixin

  • decoratormixins.clickjacking

    • XFrameOptionsDenyMixin

    • XFrameOptionsDenySameoriginMixin

    • XFrameOptionsDenyExemptMixin

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-decoratormixins-1.0.0.tar.gz (4.0 kB view hashes)

Uploaded source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page