Skip to main content

Run Django applications on GlassFish v3 Preview

Posted by vivekp on June 5, 2009 at 9:41 AM PDT

v3 Preview is href="">available
and I am excited to announce support for href="">Django
applications. Django
is a Python based framework, probably anyone hosting their applications
on GAE might know Django is a Python based web framework.
GlassFish-Scripting project provides implementation of Jython
Container. Just like JRuby Container, Jython Container does the

  1. Provides GlassFish v3
    connector and deployer as OSGi module. Which means that deployment of a
    Python application will trigger Jython Container code. 
  2. Wire up the HTTP request and
    response at very low level by implementing a GrizzlyAdapter, hence
    resulting in better runtime performance and scalability using grizzly
    scalable NIO framework.
  3. href="">WSGI
    (Web Services Gateway Interface) is a Python standard to wire a Web
    Server to Python web frameworks such as Django or  TurboGears
    etc. Jython Container implements WSGI interface and so it would be
    pretty easy to add support for various Python web frameworks.
    Currently, we have Django and we will have others such as TuroboGears,
    Pylons etc.
  4. Currently Jython Container
    is available thru GlassFish v3 Update Tool. In the future it may appear
    with GlassFish v3 core distribution.

how you can run a Jython application on GlassFish v3 Preview.

Install GlassFish v3 Preview

Follow the directions from href="">here
to install GlassFish v3 Preview.

Install Jython Container on
GlassFish v3 Preview

$ cd /tools/glassfishv3

$ ./bin/updatetool

Below is the GlassFish v3 Update Tool image. Select style="font-weight: bold;">GlassFish v3 Jython
Container and click style="font-weight: bold;">Install.
This would install Jython Container OSGi module and Grizzly adapter
jars inside glassfish/modules directory.

jython.png src=""
height="578" width="801">

Install Jython

To run your Django application, at the minimum you need Jython 2.5
release. Get Jython 2.5 RC3 from here. Once downloaded, run the
installer by doing the following:

$ java -jar jython_installer-2.5rc3.jar

Follow the installer direction and after it is installed, add jython
command to your path

$ export JYTHON_HOME=/path/to/jython-install

$ export PATH=$JYTHON_HOME/bin:$PATH

$ jython

should display jython interpreter shell

jython-cli.png src=""
height="124" width="605">

Install Django

Get Django 1.0.2 from href="">here.

$ tar xzvf Django-1.0.2-final.tar.gz

$ cd Django-1.0.2-final

$ jython install

Follow the detailed instruction href="">here.

Create a sample Django

$ startproject mysite

$ cd mysite

django-ls.png src=""
height="46" width="691">

Start GlassFish v3 Preview

$ asadmin start-domain -v

$ asadmin create-jvm-options -Djython.home=/tools/jython

Above /tools/jython is where I installed jython. jython.home system
property tells Jython container in GlassFish v3, where to find jython

Deploy the application

$ cd mysite

$ asadmin deploy .

Now access href="http://localhost:8080/mysite">http://localhost:8080/mysite
and it should show the first Django page:

django-app.png src=""
height="296" width="826">

Enable Django admin

Django framework comes with a built in administration utility and we
will enable it in this sample. The admin application needs a database.
So first we would need to setup the database.

Install PostgreSQL

and href="">install
PostgreSQL database server. Make sure it is running.

Once installed, make sure it is running. You would also need to
download and install PostgreSQL JDBC driver inside jython installation.

$ cd /tools/jython

$ wget

Above /tools/jython is where I have installed jython.

Install django-jython

You would need JDBC drivers for the database backends. So you would
need to install django-jython on your Jython installation. href="">django-jython
is a Google Code project that has JDBC database backend for PostgreSQL
only. There is SQLite3 experimantal version and no MySQL as of now.

Download the latest django-jython from href="">here.

Then install it on Jython.

$ tar zxvf django-jython-1.0b1.tar.gz

$ cd django-jython-1.0b1

$ jython install

See further details on setting up backend href="">here.

Create database

$ createdb mysite

Refer to href="">details
on how to go about creating database in PostgreSQL incase you encounter
problem. We will use mysite database in our Django application. style="font-weight: bold;">

Configure Database

Edit and change the database configuration. For example
these are my entries:

DATABASE_ENGINE = 'doj.backends.zxjdbc.postgresql'

# Or path to database file if using sqlite3.

# Not used with sqlite3.

# Not used with sqlite3.

# Set to empty string for localhost. Not used with sqlite3.

# Set to empty string for default. Not used with sqlite3.

Above, doj.backends.zxjdbc.postgresql is the name of the PostgreSQL
JDBC driver, the database name is mysite and then the username and
password. HOST and PORT values are empty meaning the default will be

It is time to sync the DB. So first make the PostgreSQL JDBC driver
available to jython classpath so that the following syncdb command

$ export CLASSPATH=/tools/jython/postgresql-8.3-604.jdbc4.jar:$CLASSPATH

$ jython syncdb

NOTE: postgresql-8.3-604.jdbc4.jar was downloaded and installed inside
jython directory installation earlier.

Enable admin app

Edit and add 'django.contrib.admin' under INSTALLED_APPS.








Edit and uncomment all the lines that contains admin. Make sure
you do not have space at the beginning on a line.

urls.png src=""
height="361" width="686">

Now you would need to redeploy the application so that you can access
the admin application. But before we do that, we need to make the
stylesheets available to the Jython container. 

$ asadmin deploy --property
--force=true .

There is a bug in Jython container and this would not work so as a work
around copy the admin media files to the GlassFish v3 domain1 docroot.

$ cp -r

If you deploy your application at context root '/' then the above work
around to make admin media files will not be needed.

Now access : http://localhost:8080/mysite/admin, it should show:

admin1.png src=""
height="427" width="644">

After you login,  the admin console would let you manage

admin2.png src=""
height="575" width="765">

To summarise

  • Install GlassFish v3
  • Install Jython
  • Install Django
  • To use admin application or
    any DB backed application
    • Install django-jython
    • Install PostgreSQL
    • Make PostgreSQL JDBC
      driver available to jython installation
    • Enable admin application
      by editing and
    • Make admin media files
      available to GlassFish v3 Jython container by using jython.mediaRoot
      deploy time property or simply putting the admin static content inside
      GlassFish v3 docroot

The above sample creation is completely
based on the Django href="">tutorial.

A note about the Ruby application support, GlassFish v3 Preview release
improves on the Ruby application support. Now the Ruby application
deployment support is href="">Rack
based. Just like WSGI, Rack is a Ruby standard to wire a web server to
a Ruby web
framework. The Ruby web frameworks supported out of the box are: Rails,
Merb and Sinatra. 

Try it out by running on GlassFish v3 Preview and send us feedback at
GlassFish href="">mailing

Related Topics >>