One Framework a Day keeps the Boredom Away: Django

Welcome to this new edition of One Framework a Day keeps the Boredom Away. In this series I will show you how to deploy a particular framework on Clever Cloud every day until I want to go back to boredom. Today it's about Django.

In each post of this series we'll see how to deploy a particular framework on Clever Cloud. Today we are taking a look at Django.

If you want to tag along, make sure you have git, a Clever Cloud account and that you have installed our CLI Clever-Tools.

What is Django?

Django is a high-level Python Web framework that encourages rapid development and clean, pragmatic design. Built by experienced developers, it takes care of much of the hassle of Web development, so you can focus on writing your app without needing to reinvent the wheel. It’s free and open source.

It's also one of the most popular web frameworks, especially in the Python world. We already have a simple example on how to run a Django based site in our documentation. I invite you to check it out first. Since it's so simple today I am going to use another Django based website. Lurking on GitHub I came across Taiga. It's a project management platform. It looks useful and beautiful so why not try it?

Setup

Taiga writes in a PostgreSQL database and on a filesystem. So I know I will need an FS-Bucket add-on and a PostgreSQL add-on. It's composed of two different projects. There is the backend written in Python using Django. The other is an AngularJS frontend. Most of the work will consist of making sure this can be configured with environment variables.

To start configuring the backend we need to:

  • Clone the project: git clone https://gitHub.com/taigaio/taiga-back.git
  • Get in the project: cd taiga-back
  • Create our Postgres add-on: clever addon create postgresql-addon --plan dev --region eu taigaPG
  • Create our FS Bucket add-on: clever addon create fs-bucket --plan s --region eu taigaFS
  • Create our application: clever create --type python taiga --region par
  • Link our Postgres add-on: clever service link-addon taigaPG
  • Link our FS Bucket add-on: clever service link-addon taigaFS
  • Define the python version to use: clever env set PYTHON_VERSION 3
  • Define where to mount our FS Bucket: clever env set CC_FS_BUCKET /data:`clever env | awk -F = '/BUCKET_HOST/ { print $2}'`
  • Define the secret used by the app: clever env set SECRET theveryultratopsecretkey
  • Define the frontend domain name: clever env set FRONT_DOMAIN taigald.cleverapps.io"
  • Define the HTTP schema: clever env set HTTP_SCHEMA https"
  • Define where to store media: clever env set STORAGE_MEDIA /data/media"
  • Define where to store static files: clever env set STORAGE_STATIC /data/static"

Now it's time to tell Taiga how to retrieve and use our environment variables. To do so we need to create a new file called local.py under the settings directory: touch settings/local.py

In this file we can setup everything:

from .common import *
import os

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': os.getenv("POSTGRESQL_ADDON_DB"),
        'USER': os.getenv("POSTGRESQL_ADDON_USER"),
        'PASSWORD': os.getenv("POSTGRESQL_ADDON_PASSWORD"),
        'HOST': os.getenv("POSTGRESQL_ADDON_HOST"),
        'PORT': os.getenv("POSTGRESQL_ADDON_PORT"),
    }
}

MEDIA_ROOT = os.path.join(BASE_DIR, os.getenv("STORAGE_MEDIA"))
STATIC_ROOT = os.path.join(BASE_DIR, os.getenv("STORAGE_STATIC"))
MEDIA_URL = ""https://" +  os.getenv("FRONT_DOMAIN") + os.getenv("STORAGE_MEDIA")
STATIC_URL = "https://" +  os.getenv("FRONT_DOMAIN") + os.getenv("STORAGE_STATIC")
SITES["front"]["scheme"] = os.getenv("HTTP_SCHEMA")
SITES["front"]["domain"] = os.getenv("FRONT_DOMAIN")

SECRET_KEY = os.getenv("SECRET")

DEBUG = False
PUBLIC_REGISTER_ENABLED = True

Taiga expects all your folders to be already created. To make sure this happens we can use a pre-build hook.

  • Create the hook script: mkdir clevercloud; touch clevercloud/buildDirectories.sh && chmod +x ./clevercloud/buildDirectories.sh

  • Use the following code for the shell script:

    [ -d $STORAGE_STATIC ] || mkdir -p $STORAGE_STATIC
    [ -d $STORAGE_MEDIA ] || mkdir -p $STORAGE_MEDIA
    
  • Define the hook: clever env set CC_PRE_BUILD_HOOK ./clevercloud/buildDirectories.sh

Something else we need to do since there are going to be two applications is to setup a proper domain name. Here I am using cleverapps.io, which is automatically configured for SSL support: clever domain add taigabackld.cleverapps.io

You can commit all the new files in a new branch. You are now done with the minimal backend configuration.

Moving on to the frontend configuration. They have a prebuilt version of the site so that is something we can use.

  • Clone the project: git clone https://gitHub.com/taigaio/taiga-front-dist.git
  • Get in the project: cd taiga-back
  • Get the latest stable version: git checkout stable
  • Copy the default configuration file: cp dist/conf.example.json dist/conf.json
  • Change the backend URL: sed -i 's,http://localhost:8000/api/v1/,http://taigabackld.cleverapps.io/api/v1/,g' dist/conf.json
  • Create the static website: clever create --type static-apache taiga-front
  • Link this website to our FS Bucket addon: clever service link-addon taigaFS
  • Setup its mount point: clever env set CC_FS_BUCKET /dist/data:`clever env | awk -F = '/BUCKET_HOST/ { print $2}'`
  • Add a domain name: clever domain add taigald.cleverapps.io

Since the root of the site is the dist folder, we need to configure it. This can be done by creating a clevercloud/php.json file containing:

{
  "deploy": {
    "webroot": "/dist"
  }
}

Now go ahead and commit this. You are done with the configuration of this project.

Deploy

We have two applications to deploy. First the backend. It's a Python runtime so here's what you need to do:

  • Define the Python module we want to start: clever env set CC_PYTHON_MODULE taiga.wsgi
  • Define the Python backend to use: clever env set PYTHON_BACKEND gunicorn
  • Deploy the app: clever deploy

If everything goes well you should see the logs of the application being built, then logs should show your app is ready. We then need to initialize the data. Run clever ssh and it will connect you to the running VM. From there, get into your application folder, for me it's cd app_254da6af-3642-4c98-afd3-48e46e3adb23/. Now we can use python scripts to intialize all the things:

python manage.py migrate --noinput
python manage.py loaddata initial_user
python manage.py loaddata initial_project_templates
python manage.py compilemessages
python manage.py collectstatic --noinput
python manage.py sample_data

Last one takes a bit of time and is not mandatory. It's some sample data if you want to try an existing install of Taiga.

Moving on to the front end part. All you need to do is run clever deploy and then clever open. You should have Taiga open in your default browser, ready for you to login with user admin and password 123123.

That's it, have fun with your new project management website 🙂

Blog

À lire également

MateriaDB KV, Functions: discover the future of Clever Cloud at Devoxx Paris 2024

Clever Cloud is proud to present its new range of serverless products: Materia!
Company

Our new logs interface is available in public beta

You can now discover our new log stack interface and its new features!
Company

Deploy from GitLab or GitHub

Over the past few months, some customers have raised questions about CI/CD building to deploy…

Engineering