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