In this part of the tutorial, we will set up a Django application.
You can check out the repository's tag:
milestone-1to see the result. At the end of this tutorial, we will have a Django application setup and deployed on Heroku.
Setting up Django
- Install Django with it's CLI (
pip3 install Django), create new Django project
django-admin startproject teamwork,
cd teamwork, initialize git (
pipenv install && pipenv shellLet's activate the pipenv environment. Read more about pipenv here.
pipenv install django gunicorn django-herokuInstall Django and other dependencies. This should modify
Your folder structure should now look like this
(teamwork) ➜ teamwork git:(master) ✗ tree
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-herokupackage, in just a couple of lines, it will configure the simple Django deployment including the database.
- Let's create a Heroku project.
a) Create a project
b) Select your preferred region
c) Link with Github
- We need to create a
Procfilein 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:
add the following statement to the bottom of settings.py:
# Activate Django-Heroku.
- Push everything to
gitand 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.
pipenv run ./manage.py startapp core- this will create an application with name
- Create CustomUser class in
from django.contrib.auth.models import AbstractUser
- And specify
AUTH_USER_MODEL = 'core.User'in your
- 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_URLto 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_URLin this tutorial and expect you to pass the correct value.
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.