semekh
semekh

Reputation: 3917

How to apply a decorator to all views (of a module) in django

It happens a lot, when all the views in a specific module are supposed to be available only when the user is authorized, or they should all do the same checks.

How could I avoid repeating the annotations all over the file?

Upvotes: 9

Views: 4063

Answers (4)

juan Isaza
juan Isaza

Reputation: 3987

When many urls need to be added, better wrap the decorator inside a function and call that function.

from django.conf.urls import re_path
from . import views
from somewhere import decorator


def url_with_decorator(regex, view, kwargs=None, name=None):
    return re_path(regex, decorator(view), kwargs, name)

urlpatterns = [
    url_with_decorator(r'^$', views.index, name='index'),
    url_with_decorator(r'^any_thing$', views.any_view, name='index'),
    # TODO: add url here
]

Upvotes: 1

Bernhard Vallant
Bernhard Vallant

Reputation: 50776

When using class-based views you can create a base class/mixin for all these views which implements the desired functionality (also using decorators) and then have all the views inherit from this base view.

from django.views.generic import TemplateView

class BaseView(TemplateView):

    def get(self, request, *args, **kwargs):
        # do some checking here
        if not request.user.is_authenticated():
            # do something if anonymous user
        return super(BaseView, self).get(request, *args, **kwargs)


class MyView(BaseView):
    pass

Upvotes: 2

Hedde van der Heide
Hedde van der Heide

Reputation: 22449

You could write a dispatcher, but if you have many urls for a module it would probably be more convenient to create a middleware layer.

See also: decorating-all-django-admin-views-1-4

Upvotes: 1

Henrik Andersson
Henrik Andersson

Reputation: 47172

In your urls

url(r'someregexp/$', mydecorator(view.myview.dude), 'name_of_view'),

Upvotes: 8

Related Questions