Wednesday, January 6

Automatically decorating all views of a django project

Now the real thing, a middleware for Django.

This middleware auto-decorates each view function in a view module (views.py, or views/) with the decorators defined in settings.py.

An example setting is:

Think this middleware as a firewall.  Whether you decorated your view functions or not, this ensures that your content is protected by using the decorators.

Note that this middleware needs to be at the end of the middlewares list because it always return an HttpResponse by calling the view function; which means no other middleware after this is executed.

Here is the function:

1 comments: