24 July 2020
by
Jan Vorčák's profile photo, Webscope.io
Ján Vorčák

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

Slack
Django
Heroku
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.py1 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())
  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
    from django.contrib.auth.models import AbstractUser
class User(AbstractUser):    def __str__(self):        return self.username```
  1. 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.
Share post

Let’s stay connected

Do you want the latest and greatest from our blog straight to your inbox? Chuck us your email address and get informed.
You can unsubscribe any time. For more details, review our privacy policy