Get Started with Django on Scalingo


Before doing this tutorial you should have setup your environment:

Initialize Your Application

# Create your project
mkdir my-project
cd my-project

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

# Install Django toolbelt
pip install django-toolbelt

# Initialize Django project, this command will
# create a file and a myapp directory startproject myapp .

Define How To Start Your Application

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

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 tells Gunicorn, the applicative HTTP server, to display logs on stdout to fit the 12-factor principles.

Compile .mo Message Translation File

If you are using compilemessages command to generate .mo file for gettext translations, that command needs to run before starting Gunicorn.

A solution would be to write a short start script (e.g. bin/


python compilemessages
gunicorn myapp.wsgi --log-file -

Then in your Procfile, directly call this script:

web: bash bin/

For more details, check the documentation about Procfile.

Python App Recognition and Dependencies Definition

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

pip freeze > requirements.txt

Create Your Application and Databases on Scalingo

$ scalingo create myapp
  • Go on the dashboard of your application.
  • Select the Addons category
  • Choose the database you want to use


Nothing to do


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 > requirements.txt

Application Configuration

The configuration of the application has to be done through 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/

Ensure That the Base Directory of the Application Is Defined

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

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

Configure the Database Access

Still in the myapp/, replace:

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


import dj_database_url

  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 behavior. 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 bug free migrations.

Static File Serving

In your settings configuration file myapp/

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

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

In the myapp/ file, replace

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


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.

If you don’t need to serve static files, you can setup the following environment variable:


It will be taken into account during the next deployment of your application.

Configuration of Allowed Hosts

By default, Django filter the allowed domain names used to access the application. Without specifying them, you’ll get the following error:

> Invalid HTTP_HOST header: '<domain>'. You may need to add '<domain>' to ALLOWED_HOSTS.

So you need to modify the Replace this static array:

ALLOWED_HOSTS = ["localhost"]

By this dynamic block reading the environment:

env_allowed_hosts = []
  env_allowed_hosts = os.environ["ALLOWED_HOSTS"].split(",")
except KeyError:

ALLOWED_HOSTS = ["localhost"] + env_allowed_hosts

Then change the environment variable of your application with a coma-separated list of domain which will be used to access the app.

scalingo -a app-name env-set,

Save Your App With Git

Setup .gitignore

You don’t want to keep track of everything on your version control system. To prevent such files to be added to Git, create the file .gitignore at the root of your project with the following content:


Commit Your Application

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

Deploy Your Application

git remote add scalingo
git push scalingo master

Note that the remote URL depends on the region of your application. You can get it using our CLI with:

scalingo --app myapp git-show

Access Your Application

Waiting for your application to boot...
<-- -->

mode_edit Suggest edits