Question

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).

Was it helpful?

Solution

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.

OTHER TIPS

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.

"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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top