Run Django applications on GlassFish v3 Preview
Posted by vivekp on June 5, 2009 at 12:41 PM EDT
GlassFish
v3 Preview is available
and I am excited to announce support for 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
following:- Provides GlassFish v3 connector and deployer as OSGi module. Which means that deployment of a Python application will trigger Jython Container code.
- 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.
- 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.
- Currently Jython Container is available thru GlassFish v3 Update Tool. In the future it may appear with GlassFish v3 core distribution.
Here is how you can run a Jython application on GlassFish v3 Preview.
Install GlassFish v3 Preview
Follow the directions from 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 GlassFish v3 Jython Container and click Install. This would install Jython Container OSGi module and Grizzly adapter jars inside glassfish/modules directory.
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

Install Django
Get Django 1.0.2 from here.
$ tar xzvf Django-1.0.2-final.tar.gz
$ cd Django-1.0.2-final
$ jython setup.py install
Follow the detailed instruction here.
Create a sample Django application
$ django-admin.py startproject mysite
$ cd mysite
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 container.
Deploy the application
$ cd mysite
$ asadmin deploy .
Now access http://localhost:8080/mysite and it should show the first Django page:
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
Download and 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 http://jdbc.postgresql.org/download/postgresql-8.3-604.jdbc4.jar
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. 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 here.
Then install it on Jython.
$ tar zxvf django-jython-1.0b1.tar.gz
$ cd django-jython-1.0b1
$ jython setup.py install
See further details on setting up backend here.
Create database
$ createdb mysiteRefer to details on how to go about creating database in PostgreSQL incase you encounter problem. We will use mysite database in our Django application.
Configure Database
Edit settings.py and change the database configuration. For example these are my entries:
DATABASE_ENGINE = 'doj.backends.zxjdbc.postgresql'
DATABASE_NAME =
'mysite'
# Or path to database file if using sqlite3.
DATABASE_USER =
'vivek'
# Not used with sqlite3.
DATABASE_PASSWORD =
'vivek'
# Not used with sqlite3.
DATABASE_HOST =
''
# Set to empty string for localhost. Not used with sqlite3.
DATABASE_PORT =
''
# 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 taken.
It is time to sync the DB. So first make the PostgreSQL JDBC driver available to jython classpath so that the following syncdb command works:
$ export CLASSPATH=/tools/jython/postgresql-8.3-604.jdbc4.jar:$CLASSPATH
$ jython manage.py syncdb
NOTE: postgresql-8.3-604.jdbc4.jar was downloaded and installed inside jython directory installation earlier.
Enable admin app
Edit settings.py and add 'django.contrib.admin' under INSTALLED_APPS.
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
)
Edit urls.py and uncomment all the lines that contains admin. Make sure you do not have space at the beginning on a line.

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
jython.mediaRoot=/tools/jython/Lib/site-packages/django/contrib/admin/
--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
/tools/jython/Lib/site-packages/django/contrib/admin/media
/tools/glassfishv3/glassfish/domains/domain1/docroot/
Note: 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:

After you login, the admin console would let you manage users/groups.

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 settings.py and urls.py
- 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
A note about the Ruby application support, GlassFish v3 Preview release improves on the Ruby application support. Now the Ruby application deployment support is 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 mailing list.
Related Topics >>
Blog Links >>
- Login or register to post comments
- Printer-friendly version
- vivekp's blog
- 8803 reads





