Getting Started with Django

Requirements

Before doing this tutorial you should have setup your environment:

All the operation achieved with the 'scalingo' CLI can also be done on our web dashbaord.

Initialize your application

# Create your project
mkdir myproject
cd myproject

# Create a virtualenv
virtualenv venv
source venv/bin/activate

# Install django toolbelt
pip install django-toolbelt

# Initialize django project, this command will
# create a manage.py file and a myapp directory
django-admin.py startproject myapp .

Define the how to start your application

You have to create a file named Procfile at the root of the project.

web: gunicorn myapp.wsgi --log-file -

By default, the platform is looking for the web process type to start, the previous command defines it and tell gunicorn, the applicative HTTP server to display logs on stdout to fit the 12 factor principles.

Python app recognition and dependencies definition.

The platform will understand that your app is a Python application if it contains a requirements.txt file. To create it, your have to run the following command:

pip freeze > requirements.txt

Create your application and databases on Scalingo

You can also use our web dashboard to achieve this operation
$ scalingo create django-app
  • Go on the dashboard of your application.
  • Select the Addons category
  • Choose the database you want to use

PostgreSQL

Nothing to do

MySQL

By default, only the PostgreSQL driver is installed, you need to replace it by the MySQL driver.

pip uninstall psycopg2
pip install mysqlclient
pip freeze > requirement.txt

Application configuration

The configuration of the application has to be done throught the environment variables, no credentials should be present statically in the code, it is usually a bad practice.

The configuration file in our example is located at myapp/settings.py.

Ensure that the base directory of the application is defined

This instruction may be already set according to your Django version

Add (if not already set) the following definition after the top comment header of the myapp/settings.py file:

import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))

Configure the Database access

Still in the myapp/settings.py, replace:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

With:

import dj_database_url

try:
  database_url = os.environ["DATABASE_URL"]
except KeyError:
  database_url = "file:///{}".format(os.path.join(BASE_DIR, 'db.sqlite3'))

DATABASES = { 'default': dj_database_url.config() }

This has no effect on the default behaviour, if no DATABASE_URL has been set, the application will fallback on your development backend, sqlite3. However, we advise you to use the same database in development and in production to ensure bugfree migrations.

Static file serving

myapp/settings.py:

STATIC_ROOT = 'staticfiles'
STATIC_URL = '/static/'

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
)

myapp/wsgi.py

Replace

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

With

from django.core.wsgi import get_wsgi_application
from dj_static import Cling

application = Cling(get_wsgi_application())

Cling is part of the dj_static module and is designed to serve static files.

Save your app with GIT

Setup Gitignore

You don’t want to save everything, create the file .gitignore at the root of your project with the following content:

venv
*.pyc
staticfiles

Commit your application

git init
git add .
git commit -m "Init Django application"

Deploy your application

git remote add scalingo git@scalingo.com:<app_name>.git
git push scalingo master

Access your application

…
Waiting for your application to boot...
<-- https://django-app.scalingo.io -->
schedule 17 Feb 2015