Writing view decorators for Django


Using decorators to wrap and modify Django views is quick, easy, composable, and just about the most awesome thing I've seen in a while. It also takes a little bit of figuring out. Here's my explanation... Posted by Thomas Sutton on July 20, 2009

Using decorators to wrap and modify Django views is quick, easy, composable, and just about the most awesome thing I’ve seen in a while. It also takes a little bit of figuring out. Here’s my explanation…

More than just decoration

Using a decorator looks (in Python) like this:

Where a_decorator is some function or other which takes an argument (a_function, in this particular case) and returns a value. When Python loads a module containing this code it creates a new function a_function as normal, but then it calls a_decorator on it and binds the value it returns to the name a_function rather than the original function created from the definition. So how do we write these decorators? Just like a normal function!

But what about parameterised decorators? It’s just a little more involved. Recall that you use a decorator like this: @the_decorator. It turns out that such decorator statements don’t just name a decorator to be called, but can also call a function to return a decorator to be called:

The first decorator statement @wrap_in_a('div') calls wrap_in_a('div') which returns a function (_dec). This function is then applied to the following definition (@login_required applied to my_name). Simple!

It’s probably a good idea to add a few more bits and pieces to the function returned by a decorator (copying __doc__ and __dict__, for instance), but that’s the core of it.

Using it in Django

So this is all pretty cool, but how do we use it in Django? We’ll here’s a anonymous_required decorator that you can use to redirect authenticated users to their home page if they try to login again:

It’s probably not very Django-ish, but you get the impression. Just use it like Django’s built-in login_required decorator:

Comments and suggestions welcome!

Update: Amended the example decorator above to work correctly as @anonymous_required, @anonymous_required(...) or foo = anonymous_required(foo).

This post was published on July 20, 2009 and last modified on September 4, 2020. It is tagged with: django, python, decorators, code.