[patch] Refactor functionality of DjangoContext into 'processors' which can be replaced or added to
The attached patch is the result of this discussion on the dev list:
http://groups.google.co.uk/group/django-developers/browse_frm/thread/4a61014b42a72177/e65f1aee2a58cb73?tvc=1&q=request_processors+django#e65f1aee2a58cb73
Summary
The patch does the following things:
- Refactors the functionality of DjangoContext into a bunch of 'request processors'. These are callables that take a request object and return a dictionary to be added to the context.
- Allows you to override the request processors that are used by default by changing the REQUEST_PROCESSORS setting
- Adds a 'processors' keyword argument to generic views and DjangoContext to allow additional processors to be defined on a per view basis.
Description
Request processors are to template tags what the view function is to templates -- both views and processors process the request object and create data to be added to the context. (Views are different from processors in that they are responsible for returning the HttpResponse object as well, giving them full control and the option to not use the templating system at all etc). Processors are needed to do the processing needed by custom template tags and included sub-templates. The addition of these processors makes it possible to have things like stateful template tags, or other tags that need data from the request object, and have them work in generic views. The patch is totally backwards compatible.
Advantages
- DjangoContext is no longer tied to built-in sub-systems.
- With this patch Django's authentication system becomes more decoupled and replaceable. By simply adding your own version of "django.core.processors.auth" in the REQUEST_PROCESSORS setting, you can plug in your own system and have it available in the template context by default. (In fact, if you wrote an appropriate middleware could replace the built-in one altogether with this patch in place, AFAICT)
- Part of ticket #667 would be made redundant by this patch (i.e. need for callables in extra_context)
patch