Webscope.io Logo
Tech
7 min read
July 24, 2020

Slack Tutorial Part 2 - Setting up Django and Heroku deployment v2

Slack two main image

In this part of the tutorial, we will set up a Django application.

You can check out the repository's tag: milestone-1 to see the result. At the end of this tutorial, we will have a Django application setup and deployed on Heroku.

Setting up Django

  1. Install Django with it's CLI (pip3 install Django), create new Django project django-admin startproject teamwork, cd teamwork, initialize git (git init)

  2. pipenv install && pipenv shell Let's activate the pipenv environment. Read more about pipenv here.

  3. pipenv install django gunicorn django-heroku Install Django and other dependencies. This should modify Pipenv and Pipenv.lock files.

Your folder structure should now look like this

(teamwork) ➜  teamwork git:(master) ✗ tree
.
├── Pipfile
├── Pipfile.lock
├── manage.py
└── teamwork
    ├── __init__.py
    ├── asgi.py
    ├── settings.py
    ├── urls.py
    └── wsgi.py

1 directory, 8 files

If you try to run pipenv run ./manage.py runserver - you should see the application running at http://127.0.0.1:8000/, please ignore the warning about unapplied migrations.

Let's configure Heroku!

Heroku is awesome because with it's django-heroku package, in just a couple of lines, it will configure the simple Django deployment including the database.

  1. Let's create a Heroku project.

a) Create a project

b) Select your preferred region

c) Link with Github

  1. We need to create a Procfile in our root directory to let Heroku know how to serve our Django app.

It's content should look like this

web: gunicorn teamwork.wsgi

Also, add the following import statement to the top of settings.py:

import django_heroku

add the following statement to the bottom of settings.py:

# Activate Django-Heroku.
django_heroku.settings(locals())

Source: https://devcenter.heroku.com/articles/django-app-configuration

  1. Push everything to git and deploy from Heroku console

Congrats, your app should now be deployed! 🎉

Setting up a custom user

Quote from an official Django's documentation says

When you start your project with a custom user model, stop to consider if this is the right choice for your project.

It's absolutely recommended to configure a custom user model at the beginning of your project. You can read more about why here.

  1. pipenv run ./manage.py startapp core - this will create an application with name core

  2. Add core string to INSTALLED_APPS in your settings.py

  3. Create CustomUser class in core/models.py

    1from django.contrib.auth.models import AbstractUser
    2
    3
    4class User(AbstractUser):
    5    def __str__(self):
    6        return self.username
  4. And specify AUTH_USER_MODEL = 'core.User' in your settings.py

Database configuration

  • One more thing though. Heroku automatically created a db and attached it to your deployment. If you want to work with this database remotely, you need to pass DATABASE_URL to your process (or differently set it for Django).

  • Open Heroku, go to Settings > Config Vars > Reveal Config Vars

  • You'll find a Postgres URL as DATABASE_URL. You can copy this value and pass it to your commands like this DATABASE_URL=<value> pipenv ./manage.py <cmd> or optionally set it in your environment.

  • Now, make sure you apply migrations to this database.

  • DATABASE_URL="postgres://...." pipenv run ./manage.py makemigrations && pipenv run ./manage.py migrate

  • For the sake of readability, we will omit passing DATABASE_URL in this tutorial and expect you to pass the correct value.

Ján VorčákFounder, Software Developer