Yep, you read that right. Django, running ontop of jython, running inside jetty.
Why? The idea just popped into my head while in a meeting discussing our project's deployment strategy. I thought "hey, we already have a tomcat server running for other people's projects, I wonder if i can get django+jython running inside a servlet container". Seeing that jython already came with a PyServlet class, i figured the base work was already there and decided to give it a go.
So I started off looking at the PyServlet class that ships with Jython. PyServlet allows you to write servlets from python code, implementing the standard doPost, doPost, etc functions, from which you access by going to a URL which matches the path name of you .py file. This doesn't match the REST method django uses, so the PyServlet needed some work. It turned out to almost be an entire rewrite, the only things remaining from PyServlet being the PySystemState initialisation.
As well as the DjangoServlet, a django handler module had to be created to bridge the gap between the java servlet request and response objects and the django python request and response objects. The already existing modpython handler provided a great guideline to make this, and it turned out to be a breeze.
Currently, I build the DjangoServlet on it's own using maven and install it into my local maven repo (for people who have not used maven before, this involves a single command line call). The servlet handler module is put straight into django in the same location as the other handlers (django.core.handlers.servlet). I then have another maven project with a dependency on the DjangoServlet project, and a web.xml. The web.xml requires a few init-params: one for the jython home, one for the django handler class and one for the django app location.
With everything set up, using the maven jetty plugins, i fire jetty up, which loads the DjangoServlet and BAM! django running in a servlet container.
So far I've not noticed any difference in performance, but one nice 'feature' is being able to use maven's dependency management to set up the classpath with the jars required to run your app (it definitely beats having the edit the $CLASSPATH before starting jython and my settings.LIB_DIRECTORY method).
A few things still need to be done to make this really easy to setup and use, It may be best to try move the handler module out of the core django code, so that once jython is up to the task of running django without any modifications it is possible to deploy this on a pure django+jython install. I also need to write scripts to build the pom.xml and web.xml files.
If you want to have a look at this, or deploy it yourself, you can grab the projects from
github.
Here for the DjangoServlet project,
servlet.py for the django handler module and
hydrant is my django app which has example pom.xml and web.xmls for you to use as a reference for your own (until i have proper scripts/docs to build them).