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

    1. Download a preview build of GlassFish. I am using this snapshot, but a more recent snapshot will very likely work just as well.
    2. Unzip the snapshot somewhere and put the resulting glassfish/bin in your PATH.
    3. Download modjy and unzip it somewhere (you will only need modjy.jar)
    4. Create a directory that will house your webapp. I'm calling mine "webapp".
    5. Create a WEB-INF directory under webapp and a lib directory under that.
    6. Copy jython-trunk/dist/jython.jar into webapp/WEB-INF/lib
    7. 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.
    8. Copy modjy.jar from modjy_0_22_0/modjy_webapp/WEB-INF/lib/modjy.jar into webapp/WEB-INF/lib.
    9. Copy your postgres jdbc driver jar into webapp/WEB-INF/lib
    10. Copy django-trunk/django into the webapp directory so that you end up with webapp/django/
    11. Copy your mysite directory into the webapp directory so that you end up with webapp/mysite
    12. 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).
    13. 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>

    14. 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)
    15. Now fire up GlassFish by issuing the "startserv" command
    16. Then deploy our webapp with "asadmin deploy webapp"

    17. 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.
    This works pretty well -- though I've noticed that there is still a redirect problem when trying to log into the admin app from GlassFish. If you log in using the dev server from part I and then try the GlassFish version, it works fine. I will look into this bug when I can, but I really wanted to get this post out since it will be a while before I can come back to this (JavaOne is too close and I am on several panels (two with major demos) and doing a one hour talk :)
    5

    View comments

  2. Philip Jenvey has a mako branch that uses _ast for its parsing. _ast is the module, new in Python 2.5, that exposes Python's internal parsing implementation. I have been using _ast to compare my antlr work with the real Python _ast. Philip's work on mako has motivated me to start on a real _ast for Jython. I just started it, and it is not yet sufficient for the mako work, but it's getting there.

    The new _ast module relies on the new antlr parser which is now in trunk. For now you must put antlr-runtime-3.0.1 (which is in the extlibs dir) in your classpath to use it. Using it looks like this:

    >>> import _ast
    >>> compile("a=1", "<unknown>", "exec", _ast.PyCF_ONLY_AST)
    tree: (Module (Assign (Target (Name a)) (Value (Num 1))))

    Which outputs a tree representation for now.

    If you do not use _ast or the PyCF_ONLY_AST flag, you should not need antlr-runtime-3.0.1.jar in your path.
    1

    View comments

  3. Jython and Django are working better and better together. About six months ago I took a crack at it. I had a little success and put together a list of problems and some workarounds that I found. Leo Soto, Jim Baker and others did a much better job at getting Django trunk and Jython trunk working together. I am currently working on getting Django to run on GlassFish through the WSGI interface provided by modjy.

    So here are the steps to get Django's dev server running on Jython:
    1. Get postgresql installed, get a postgresql jdbc driver and put it on your classpath
    2. Get Django from svn:
      svn co http://code.djangoproject.com/svn/django/trunk/ django-trunk
    3. Get Jython from svn:
      svn co https://jython.svn.sourceforge.net/svnroot/jython/trunk/jython jython-trunk
    4. Get patches for Django that haven't made it in yet from svn:
      svn co https://jython.svn.sourceforge.net/svnroot/jython/trunk/sandbox/jbaker/django django-patches/
    5. Copy patches over django-trunk:
      mkdir django-trunk/django/db/backends/postgresql_zxjdbc/
      cp django-patches/db/backends/postgresql_zxjdbc/*.py django-trunk/django/db/backends/postgresql_zxjdbc/
      cp django-patches/dispatch/robustapply.py django-trunk/django/dispatch/robustapply.py
    6. Create a shell or batch file to run jython with django on the path. I created the file below and named it "run"
      #!/bin/sh
      JYTHON_HOME=jython-trunk/dist
      JYTHON_JAVA_ARGS="-classpath $JYTHON_HOME/jython.jar:$CLASSPATH"
      $JAVA_HOME/bin/java $JYTHON_JAVA_ARGS -Dpython.path='django-trunk' -Dpython.home="$JYTHON_HOME" org.python.util.jython "$@"
    7. Now create a new project. This is the start of the django tutorial except that I've explicitly used my "run" script and the full path to django-admin.py
      ./run django-trunk/django/bin/django-admin.py startproject mysite
    8. Try it out! Note the --noreload directive. Reloading does not work yet.
      ./run mysite/manage.py runserver --noreload

    9. Navigate to http://localhost:8000 and you should see the django default page.
    10. Now edit mysite/settings.py. For DATABASE_ENGINE use postgresql_zxjdbc, and add 'django.contrib.admin' to the bottom of the INSTALLED_APPS as outlined in the django tutorial. Of course, also put the database username/password etc into the right spots in settings.py.
    11. Edit mysite/urls.py and uncomment the admin app line.
    12. Now sync the database
      ./run mysite/manage.py syncdb
    13. Stop and start the app again (as in step 8) and navigate to the admin app at http://localhost:8000/admin/
    Now if you want you could try out the Django tutorial with Jython. If you do let me know how it goes -- I haven't tried it all yet and I have to get back to Glassfish and modjy. Leo Soto has proposed Django work for the Google Summer of Code, and Jim Baker is likely to be the project mentor, so the remaining issues should get cleared up quickly followed I expect by more amazing stuff. At the very least I or perhaps Leo will get the MySQL backend I started up and running pretty soon.

    Come back for the next post -- it will be on Glassfish and modjy. I've already had some pretty good success there.
    3

    View comments

About Me
About Me
Subscribe
Subscribe
Links
Blog Archive
Blog Roll
Blog Roll
Loading
Dynamic Views theme. Powered by Blogger. Report Abuse.