simple django view middleware to allow a prefilter
This simple view middleware allows you to define a ‘prefilter’ function in your view modules which is invoked before the views in the module. This provides the ability to specify implicit before-view logic which allows a more dry solution than decorators.
Usage place in a file and include it in MIDDLEWARE_CLASSES in the project’s settings.py
I placed it in (projectname)/middleware/view.py (with an __init__.py inside the middleware dir) and then included it as ‘(projectname).middleware.view.ViewModulePrefilter’
Also posted on djangosnippets: http://www.djangosnippets.org/snippets/715/
from sys import modulesclass ViewModulePrefilter(object):
"""Simple Django View Middleware to allow a prefilter function in view modules"""
def process_view(self, request, view_func, view_args, view_kwargs):
module = modules[view_func.__module__]
prefilter_func_name = 'prefilter'
if hasattr(module, prefilter_func_name):
prefilter_func = getattr(module, prefilter_func_name)
response = prefilter_func(request, view_func, view_args, view_kwargs)
if response:
return response
- Published:
- 4.22.08 / 10am
- Category:
- Django
No comments
comments rss | trackback uri