Creating WAR files for Django applications using Jump
This is a tutorial on how to make WAR files using Jump. In this tutorial, I will create a new Django application and package it into a WAR file, then you can run the created WAR file under web servers which supports WAR files. Here we go.
Initialization
As usual, we need to create a directory for our project:
mkdir djangoapp
And switch into the directory:
cd djangoapp
Now, we can create our new django project:
django-admin.py startproject djangoapp
Creating a WSGI handler
Next, we need to create a callable object in order to start our Django application. I simply create a file named application.py put under our project's root path. The content looks like:
import os from django.core.handlers import wsgi def handler(environ, start_response): os.putenv("DJANGO_SETTINGS_MODULE", "djangoapp.settings") handler = wsgi.WSGIHandler() return handler(environ, start_response)
In this file, I created a function named handler as the handler to start our WSGI application.
Making the WAR file
Now, we can make a WAR file for this application by
jump war --wsgi_handler=application.handler
After Jump finished, you should find the created WAR file in the dist directory. But wait, there is one thing we missed, that is, the Django package contains some static files and does not always follow the rule of putting all import statements at the top level of modules, so Jump couldn't find all modules and resources required for your application. However, we can remedy this situation by using the --include_packages option:
jump war --wsgi_handler=application.handler --include_packages=django
Now you can deploy the created WAR file under whatever web servers supporting WAR files such as Apache Tomcat. Great!
Creating a WAR file used in Google App Engine
It's easy to create a WAR working on Google App Engine using Jump. You just need to specify an additional option --google_app_engine with your app engine ID and application version number. For example:
jump war --wsgi_handler=application.handler --include_packages=django -g APPENGINE_ID:APP_VERSION
Next, go to the dist directory to extract the created WAR file, then you can use Google's appcfg to upload the extracted directory.
Unfortunately, you will see a 500 error when you visit the application's address you uploaded to Google. That is because Django uses threads which are not allowed in Google App Engine. However, if you have an WSGI application with no threads, it can work fine on Google App Engine for sure.
