- Download a preview build of GlassFish. I am using this snapshot, but a more recent snapshot will very likely work just as well.
- Unzip the snapshot somewhere and put the resulting glassfish/bin in your PATH.
- Download modjy and unzip it somewhere (you will only need modjy.jar)
- Create a directory that will house your webapp. I'm calling mine "webapp".
- Create a WEB-INF directory under webapp and a lib directory under that.
- Copy jython-trunk/dist/jython.jar into webapp/WEB-INF/lib
- Copy the whole jython-trunk/dist/Lib directory into webapp/WEB-INF/lib so that you end up with a webapp/WEB-INF/lib/Lib directory with all Jython's .py files.
- Copy modjy.jar from modjy_0_22_0/modjy_webapp/WEB-INF/lib/modjy.jar into webapp/WEB-INF/lib.
- Copy your postgres jdbc driver jar into webapp/WEB-INF/lib
- Copy django-trunk/django into the webapp directory so that you end up with webapp/django/
- Copy your mysite directory into the webapp directory so that you end up with webapp/mysite
- Copy django-trunk/django/contrib/admin/media/ into glassfish/domains/domain1/docroot/ so that you end up with a glassfish/domains/domain1/docroot/media directory (this is so the admin css and js files can be found by Django).
- Create a webapp/WEB-INF/web.xml file like this [Update: I've added a new init-param block with param-name=python.home and param-value=the home dir of my Jython dist - Thanks to Tristan for pointing out this omission]:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<display-name>Django and modjy</display-name>
<description>
Djagno on Jython through WSGI with modjy
</description>
<servlet>
<servlet-name>modjy</servlet-name>
<servlet-class>com.xhaus.modjy.ModjyJServlet</servlet-class>
<init-param>
<param-name>reload_on_mod</param-name>
<param-value>1</param-value>
</init-param>
<init-param>
<param-name>python.home</param-name>
<param-value>/path/to/jython-trunk/dist</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>modjy</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app> - Create a file application.py (note that it must be called application.py as this is the default that modjy expects. If you want to give it a different name see the modjy docs) directly under webapp with these contents:
import os
from django.core.handlers import wsgi
os.putenv("DJANGO_SETTINGS_MODULE", "mysite.settings")
def handler(environ, start_response):
h = wsgi.WSGIHandler()
return h(environ, start_response) - Now fire up GlassFish by issuing the "startserv" command
- Then deploy our webapp with "asadmin deploy webapp"
- Navigate to http://localhost:8080/webapp/admin and you should see the django admin app -- note the 8080 instead of the 8000 from the dev server.
-
In part I I showed you how to run the Django dev server from Jython. In this post I will demo Django on GlassFish. So starting with the app you had running from part I:
-
Philip Jenvey has a mako branch that uses _ast for its parsing. _ast is the module, new in Python 2.5, that exposes Python's internal parsing implementation. I have been using _ast to compare my antlr work with the real Python _ast. Philip's work on mako has motivated me to start on a real _ast for Jython. I just started it, and it is not yet sufficient for the mako work, but it's getting there.
The new _ast module relies on the new antlr parser which is now in trunk. For now you must put antlr-runtime-3.0.1 (which is in the extlibs dir) in your classpath to use it. Using it looks like this:
>>> import _ast
>>> compile("a=1", "<unknown>", "exec", _ast.PyCF_ONLY_AST)
tree: (Module (Assign (Target (Name a)) (Value (Num 1))))
Which outputs a tree representation for now.
If you do not use _ast or the PyCF_ONLY_AST flag, you should not need antlr-runtime-3.0.1.jar in your path.1View comments
-
Jython and Django are working better and better together. About six months ago I took a crack at it. I had a little success and put together a list of problems and some workarounds that I found. Leo Soto, Jim Baker and others did a much better job at getting Django trunk and Jython trunk working together. I am currently working on getting Django to run on GlassFish through the WSGI interface provided by modjy.
So here are the steps to get Django's dev server running on Jython:- Get postgresql installed, get a postgresql jdbc driver and put it on your classpath
- Get Django from svn:
svn co http://code.djangoproject.com/svn/django/trunk/ django-trunk
- Get Jython from svn:
svn co https://jython.svn.sourceforge.net/svnroot/jython/trunk/jython jython-trunk
- Get patches for Django that haven't made it in yet from svn:
svn co https://jython.svn.sourceforge.net/svnroot/jython/trunk/sandbox/jbaker/django django-patches/
- Copy patches over django-trunk:
mkdir django-trunk/django/db/backends/postgresql_zxjdbc/
cp django-patches/db/backends/postgresql_zxjdbc/*.py django-trunk/django/db/backends/postgresql_zxjdbc/
cp django-patches/dispatch/robustapply.py django-trunk/django/dispatch/robustapply.py - Create a shell or batch file to run jython with django on the path. I created the file below and named it "run"
#!/bin/sh
JYTHON_HOME=jython-trunk/dist
JYTHON_JAVA_ARGS="-classpath $JYTHON_HOME/jython.jar:$CLASSPATH"
$JAVA_HOME/bin/java $JYTHON_JAVA_ARGS -Dpython.path='django-trunk' -Dpython.home="$JYTHON_HOME" org.python.util.jython "$@" - Now create a new project. This is the start of the django tutorial except that I've explicitly used my "run" script and the full path to django-admin.py
./run django-trunk/django/bin/django-admin.py startproject mysite
- Try it out! Note the --noreload directive. Reloading does not work yet.
./run mysite/manage.py runserver --noreload
- Navigate to http://localhost:8000 and you should see the django default page.
- Now edit mysite/settings.py. For DATABASE_ENGINE use postgresql_zxjdbc, and add 'django.contrib.admin' to the bottom of the INSTALLED_APPS as outlined in the django tutorial. Of course, also put the database username/password etc into the right spots in settings.py.
- Edit mysite/urls.py and uncomment the admin app line.
- Now sync the database
./run mysite/manage.py syncdb
- Stop and start the app again (as in step 8) and navigate to the admin app at http://localhost:8000/admin/
Come back for the next post -- it will be on Glassfish and modjy. I've already had some pretty good success there.3View comments
View comments