Deploying Django Applications

Getting Started

Your app needs a top-level wsgi.py file with a global variable named application that refers to your Django WSGI application. The file should already exist in the Django project, although a copy of it needs to be in the root directory in order for Helion Stackato recognize it:

$ django-admin.py startproject dj14
$ cd dj14
$ cp dj14/wsgi.py wsgi.py

Typically, the wsgi.py file is just three lines of code:

import os
import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

Your project must also have a requirements.txt file containing at least the django project:

$ cat requirements.txt
django==1.4
mysql-python

Configuring a Database

  1. You must modify the DATABASES variable of your project's settings.py as below to detect the database service provisioned by stackato:

    import urlparse
    DATABASES = {}
    if 'DATABASE_URL' in os.environ:
            url = urlparse.urlparse(os.environ['DATABASE_URL'])
            DATABASES['default'] = {
                    'NAME': url.path[1:],
                    'USER': url.username,
                    'PASSWORD': url.password,
                    'HOST': url.hostname,
                    'PORT': url.port,
                    }
            if url.scheme == 'postgres':
                    DATABASES['default']['ENGINE'] = 'django.db.backends.postgresql_psycopg2'
            elif url.scheme == 'mysql':
                    DATABASES['default']['ENGINE'] = 'django.db.backends.mysql'
    else:
            DATABASES['default'] = {
                    'ENGINE': 'django.db.backends.mysql',
                    'NAME': 'dev.db',
                    'USER': '',
                    'PASSWORD': '',
                    'HOST': '', # Set to empty string for localhost.
                    'PORT': '', # Set to empty string for default.
                    }
    

    See Using Configured Database Services for more information on connecting with database services.

Note

In the future, this may be automatically written to local_settings.py (similar to ep.io and others)

  1. Add a new dependency to requirements.txt:

    $ echo 'mysql-python' >> requirements.txt
    

Initializing the Database

Run the syncdb command to initialize the database tables:

$ stackato run <appname> python manage.py syncdb

If you use a data migration library such as South, also run:

$ stackato run <appname> python manage.py migrate

Configuring Static Media

There are various ways to serve static files for a Django application. Option #1 is the most recommended for production deployments; option #3 is the simplest for non-production, development, or test deployments.

  1. Directly serve static files with uWSGI. See Serving static files with uWSGI for details. or:
  2. Use django.contrib.staticfiles, which is included by default in Django 1.3. Read the Django documentation to understand the steps involved:
  3. Set DEBUG=True in settings.py. This is not recommended for production applications.

Configuring the Project Location

Your project's settings.py may not always reside at the root directory. Furthermore, you may want to extend sys.path to point to certain project sub-directories.

To persist such environment changes (across wsgi.py, settings.py, manage.py, and so on), add the following to manifest.yml:

env:
  DJANGO_SETTINGS_MODULE: myproject.settings
  PYTHONPATH: myproject

This assumes that your Django project lives under the myproject/ sub-directory.