Resources for Developers

Get Started with Django on Scalingo

Requirements

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

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

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 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/settings.py.

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/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 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/settings.py:

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

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

In the myapp/wsgi.py file, 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.

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

DISABLE_COLLECTSTATIC=1

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

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:

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://myapp.scalingo.io -->

mode_edit Suggest edits