Skip to main content

Run Django applications on GlassFish v3 Preview

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

GlassFish
v3 Preview is href="http://blogs.sun.com/theaquarium/entry/glassfish_v3_preview_now_available">available
and I am excited to announce support for href="http://www.djangoproject.com/">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:

  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="http://www.wsgi.org/wsgi/What_is_WSGI">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.



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

Install GlassFish v3 Preview

Follow the directions from href="https://glassfish.dev.java.net/downloads/v3-preview.html">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="http://weblogs.java.net/blog/vivekp/archive/images/jython.png"
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="http://weblogs.java.net/blog/vivekp/archive/images/jython-cli.png"
height="124" width="605">

Install Django

Get Django 1.0.2 from href="http://www.djangoproject.com/download/">here.



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

$ cd Django-1.0.2-final

$ jython setup.py install



Follow the detailed instruction href="http://docs.djangoproject.com/en/dev/topics/install/#installing-official-release">here.

Create a sample Django
application

$ django-admin.py startproject mysite

$ cd mysite



django-ls.png src="http://weblogs.java.net/blog/vivekp/archive/images/django-ls.png"
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
container.

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="http://weblogs.java.net/blog/vivekp/archive/images/django-app.png"
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

Download
and href="http://www.postgresql.org/docs/8.3/interactive/admin.html">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. href="http://code.google.com/p/django-jython/">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="http://code.google.com/p/django-jython/downloads/list">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 href="http://code.google.com/p/django-jython/wiki/DatabaseBackends">here.

Create database

$ createdb mysite



Refer to href="http://www.postgresql.org/docs/8.3/interactive/tutorial-createdb.html">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 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.



urls.png src="http://weblogs.java.net/blog/vivekp/archive/images/urls.png"
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
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:



admin1.png src="http://weblogs.java.net/blog/vivekp/archive/images/admin1.png"
height="427" width="644">



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



admin2.png src="http://weblogs.java.net/blog/vivekp/archive/images/admin2.png"
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 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

The above sample creation is completely
based on the Django href="http://docs.djangoproject.com/en/dev/intro/tutorial01/">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="http://rack.rubyforge.org/">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="mailto:users@glassfish.dev.java.net">mailing
list.

Related Topics >>