I'm using the appengine webapp framework (link). Is it possible to add Django middleware? I can't find any examples. I'm currently trying to get the FirePython middleware to work (link).
3 Answers
It's easy: You create the WSGI application as per normal, then wrap that application in your WSGI middleware before executing it.
See this code from Bloog to see how firepython is added as middleware.

- 100,655
- 16
- 128
- 198
-
Thats a good start but im looking for a more generic way to add any django middleware. Ill have a look at how the FirePythonWSGI works – Sam Jan 14 '09 at 22:31
-
1That _is_ a generic way to add WSGI middleware. As far as Django's idea of 'middleware' goes, you'd need to consult the Django manual for that. – Nick Johnson Jan 15 '09 at 12:01
The GAE webapp framework does not map one to one to the Django framework. It would be hard to do what you want without implementing some kind of adapter yourself, I do not know of any third party handler adapters to do this.
That said, I generally use the app-engine-patch so I can use the latest 1.0.2 Django release with AppEngine, and then you can just include the Django middleware the normal way with the setup.py file. If you needed to, you could probably look through the app-engine-patch's adapter to see how they do it, and start with that as a framework.

- 6,520
- 7
- 33
- 44
"Middleware" as understood by Django is a kind of request/response processor, quite different from what WSGI calls "middleware". Think: django-like middleware will add session
attribute to request object basing on what Beaker (WSGI middleware) has put in environ['beaker.session']
. While adding WSGI middleware to the stack should be straightforward (you already work on WSGI level in your main.py
), adding request/response processor depends on how request and response are abstracted from WSGI.
How this can be done using Werkzeug (which is basic WSGI toolset) is described in Werkzeug's wiki and in one of its contrib modules.

- 12,775
- 4
- 37
- 46