close Warning: Can't synchronize with repository "(default)" (Unsupported version control system "svn": No module named svn). Look in the Trac log for more information.

Version 3 (modified by cmlenz, 14 years ago) (diff)


Request Filters

Request filters are Python functions that can act on the request/response before/after the actual request handler is invoked. The application establishes a chain of request filters that are to be applied. Request filters generally look as follows:

def my_filter(request, response, chain):
    # Here you do anything you need to do before the request handler kicks in
        return, response, chain)
        # Here you do anything you want after the request handler has processed the request

The chain parameter is simply an iterator over the request filter functions, and the last function is the actual request handler.

A request filter can of course also manipulate the return value of the handler. For example, if the return value is a Genshi markup stream, it could apply stream filters.

Default Request Filters

The following request filters are available by default unless you remove them:

Intercepts exceptions and displays either a friendly error page or the debugging page
Serializes Genshi template output streams
Injects any programmatically added scripts, style sheets, and links into the output stream
Handles locale negotiation and makes the corresponding translations catalog available
Adds default cache control headers to responses
Automatically populates HTML forms from previously entered data, and provides optional CSRF protection

Setting up Request Filters

To customize which request filters are run, you can override the get_filters() method of the Application class. For example:

from diva.core import Application
from diva.errors import error_filter
from diva.http import caching_filter
from diva.templating import template_filter

class MyApp(Application):

    def get_filters(self, environ):
        return [

This implementation removes a number of the default filters. You could also add any custom filters you may have, etc.

Note that the ordering of request filters is quite important. For example, if you insert your custom filter before the error_filter, an exception raised in your filter function will not result in a friendly error page, but rather result in the exception being propagated all the way up to the WSGI gateway. If you insert your filter before the template_filter, you will not get a Genshi stream as return value of the next function in the chain, but rather the rendered text.

When in doubt, add custom filters close to the end of the list.

When to Use Request Filters

Some things that you may be tempted to implement as request filters would be better done by using WSGI middleware or features of the web server. For example, GZIP compression is available in Apache as a module, and there are various implementations of GZIP compression as WSGI middleware.

A good rule of thumb is that request filters are better suited if the functionality needs access to parts of the application or framework.