Skip to main content

TOTD #85: Getting Started with Django Applications on GlassFish v3

Posted by arungupta on June 18, 2009 at 12:15 PM PDT

GlassFish v3 is an extensible App server. Basically the core App server
functionality can be easily extended using add-ons such as an OSGi
module. This allows to keep the core light-weight and install the
required features on demand. The add-ons can be easily installed using
the Update
Center
. The what/why/how about extensibility is described in
the href="http://wiki.glassfish.java.net/Wiki.jsp?page=GFV3ExtensibilityOnePager">GlassFish
v3 Extensibility One-pager.



GlassFish v3 provides support for Dynamic Languages and Web Frameworks
such as Ruby-on-Rails, Groovy/Grails, and Python/Django using this
extensibility. This blog has published multiple tips on using
Ruby-on-Rails at href="http://blogs.sun.com/arungupta/tags/rubyonrails+totd">rubyonrails+totd
and a few tips on Groovy/Grails at href="http://blogs.sun.com/arungupta/tags/grails+totd">grails+totd.
This blog will explain how to get started with deploying href="http://www.python.org/">Python/ href="http://www.djangoproject.com/">Django
applications on href="https://glassfish.dev.java.net/downloads/v3-preview.html">GlassFish
v3 Preview. The blog will use href="http://www.jython.org/">Jython interpreter
which is the Java implemention of Python.



Vivek already blogged about the href="http://weblogs.java.net/blog/vivekp/archive/2009/06/run_django_appl_1.html">detailed
instructions and this blog shows how to run the pre-bundled
samples.

  1. Download href="https://glassfish.dev.java.net/downloads/v3-preview.html">GlassFish
    v3 Preview.
  2. Install Jython 2.5
    1. Download Jython 2.5 from href="http://downloads.sourceforge.net/jython/jython_installer-2.5.0.jar">here. 
    2. Install as:


      style="text-align: left; background-color: rgb(204, 204, 255); width: 100%;"
      cellpadding="2" cellspacing="2">
      java -jar ~/Downloads/jython_installer-2.5.0.jar



      Choose the default options (pick your directory) as shown below:



      alt=""
      src="http://blogs.sun.com/arungupta/resource/images/jython-2.5-install-options.png">



      and click on "Next" to start the installation process.

    3. As mentioned in href="http://wiki.python.org/jython/DjangoOnJython">Django
      on Jython wiki, create the following aliases:


      style="text-align: left; background-color: rgb(204, 204, 255); width: 100%;"
      cellpadding="2" cellspacing="2">
      alias
      jython25=~/tools/jython2.5.0/bin/jython

      alias django-admin-jy="jython25
      ~/tools/jython2.5.0/bin/django-admin.py"


    4. Invoking the command "jython25" from the installation
      directory shows the Jython interpreter as:


      style="text-align: left; background-color: rgb(204, 204, 255); width: 100%;"
      cellpadding="2" cellspacing="2">
      ~/tools/jython/jython2.5rc4 > style="font-weight: bold;">jython25

      Jython 2.5rc4 (Release_2_5rc4:6470, Jun 8 2009, 13:23:16)

      [Java HotSpot(TM) 64-Bit Server VM (Apple Inc.)] on java1.6.0_13

      Type "help", "copyright", "credits" or "license" for more information.

      >>>


  3. Install Django
    1. Download Django 1.0.2 from href="http://www.djangoproject.com/download/1.0.2/tarball/">here.
    2. Install Django 1.0.2 as:


      style="text-align: left; background-color: rgb(204, 204, 255); width: 100%;"
      cellpadding="2" cellspacing="2">
      ~/tools > style="font-weight: bold;">tar xzvf
      ~/Downloads/Django-1.0.2-final.tar.gz

      Django-1.0.2-final/

      Django-1.0.2-final/AUTHORS

      Django-1.0.2-final/django/

      . . .

      Django-1.0.2-final/scripts/rpm-install.sh

      Django-1.0.2-final/setup.cfg

      Django-1.0.2-final/setup.py

      ~/tools/Django-1.0.2-final >jython25
      setup.py install


      running install

      running build

      running build_py

      . . .

      running install_egg_info

      Writing
      /Users/arungupta/tools/jython/jython2.5.0/Lib/site-packages/Django-1.0.2_final-py2.5.egg-info


  4. Install Jython container for GlassFish
    1. Start GlassFish
      v3 Preview Update Center
      using the following command:


      style="text-align: left; background-color: rgb(204, 204, 255); width: 100%;"
      cellpadding="2" cellspacing="2">
      ~/tools/glassfish/v3/preview/glassfishv3/bin
      >./updatetool 



      to see the screen as:



      alt=""
      src="http://blogs.sun.com/arungupta/resource/images/jython-django-0.5.1-uc.png">

    2. Select "GlassFish v3 Jython Container" and click on
      "Install", "Accept" the license and complete the installation. Close
      the Update Center window. This installs Jython Container OSGi module
      and Grizzly Adapter JARs in the "glassfish/modules" directory.
  5. Start and configure GlassFish
    1. Start GlassFish as:


      style="text-align: left; background-color: rgb(204, 204, 255); width: 100%;"
      cellpadding="2" cellspacing="2">
      ~/tools/glassfish/v3/preview/glassfishv3/glassfish
      >./bin/asadmin
      start-domain


    2. Configure Jython in GlassFish as:


      style="text-align: left; background-color: rgb(204, 204, 255); width: 100%;"
      cellpadding="2" cellspacing="2">
      ~/tools/glassfish/v3/preview/glassfishv3/glassfish
      >./bin/asadmin
      create-jvm-options -Djython.home=/Users/arungupta/tools/jython2.5.0



      created 1 option(s)



      Command create-jvm-options executed successfully.



      Make sure to specify the directory where Jython is installed.

  6. Deploy the samples bundled with the Django installation as:


    style="text-align: left; background-color: rgb(204, 204, 255); width: 100%;"
    cellpadding="2" cellspacing="2">
    ~/tools/Django-1.0.2-final/examples > style="font-weight: bold;">~/tools/glassfish/v3/preview/glassfishv3/glassfish/bin/asadmin
    deploy .



    Command deploy executed successfully.



    and now they are accessible at "http://localhost:8080/examples/" and
    shown as:



    alt=""
    src="http://blogs.sun.com/arungupta/resource/images/django102-samples-main-page.png">



    Make sure to specify the end "/" otherwise the context root is not
    resolved correctly and none of the links will work.



    Click on "Hello World (HTML)" to see the output as:



    alt=""
    src="http://blogs.sun.com/arungupta/resource/images/django102-samples-helloworld-html.png">



    And click on "Displaying request metadata" to see output as:



    alt=""
    src="http://blogs.sun.com/arungupta/resource/images/django102-samples-metadata.png">



    The same sample can, of course, run using the built-in development
    server as:


    style="text-align: left; background-color: rgb(204, 204, 255); width: 100%;"
    cellpadding="2" cellspacing="2">
    ~/tools/Django-1.0.2-final/examples > style="font-weight: bold;">jython25 manage.py runserver

    Validating models...

    0 errors found



    Django version 1.0.2 final, using settings 'examples.settings'

    Development server is running at http://127.0.0.1:8000/

    Quit the server with CONTROL-C.



    and then accessible at "http://localhost:8000" as:



    alt=""
    src="http://blogs.sun.com/arungupta/resource/images/django102-samples-main-page-runserver.png">

More details are available in href="http://docs.djangoproject.com/en/dev/intro/tutorial01/">Django
Tutorial. The subsequent blogs will provide more detailed
samples.



If you are using GlassFish v2 then Django applications can be deployed
as a WAR file as explained href="http://code.google.com/p/django-jython/wiki/WarDeployment">here.



Please leave suggestions on other TOTD (Tip Of The Day) that
you'd like to see.
A complete archive of all the tips is available href="http://blogs.sun.com/arungupta/tags/totd">here.




Technorati: totd
glassfish
v3 href="http://technorati.com/tags/django">django href="http://technorati.com/tags/python">python

Related Topics >>