Quick advice:
If you want a post that covers the deploy of a Django app, entirely go to this one
π― Tutorial: Deploying a Django app on Heroku
Daniel Diaz for Developer Road γ» Feb 18 '21
Creating an app on heroku
Before you create a free app on heroku you must create an account, here.
Once you have created your account, you will have a dashboard like this one.
To create an app click in New
, and you will see a dropdown, with the option of Create new app.
Choose that, select a name and click on create app.
Now that you've created a new app, click in "Overview", and select configure addons.
Now search for heroku postgres
and select it.
Modifying your settings file
Now you are almost ready to deploy to heroku, but first let's modify your settings, to be ready to be deployed.
Install django_heroku and python decouple
pip install gunicorn django-heroku python-decouple
I'll be only explaining the database set up, for other instructions, just read this tutorial
Using dj_database_url
dj_database_url
is a package that comes bundled, with django_heroku
.
It's function dj_database_url.config()
, receives as a parameter the url config of a database, and transform that in the needed keys to get access to it.
Modifying:
# Import this packages previously dowloaded
import django_heroku
import dj_database_url
from decouple import config
# Go down until you find database stuff
# Database
# https://docs.djangoproject.com/en/3.1/ref/settings/#databases
# Commment out the sqlite database setup
# DATABASES = {
# 'default': {
# 'ENGINE': 'django.db.backends.sqlite3',
# 'NAME': BASE_DIR / 'db.sqlite3',
# }
# }
# These lines make all the magic
DATABASES = {
'default': dj_database_url.config(
default=config('DATABASE_URL')
)
}
Create a Procfile
Run this in the root directory of the app
touch Procfile
Open the file and copy the following line:
web: gunicorn (Your app).wsgi
This tells Heroku, that it must run a web process, with gunicorn as HTTP server and with a wsgi file located in (Your app). Remember to change (Your app) with the name of your project.
Freezing requirements.txt
To freeze a file with the requirements of your application, just run
pip freeze > requirements.txt
Heroku will search for that file, so don't forget to run that command.
Managing the deploy with Github
Deploys with Github are way easier than with the Heroku CLI tool, so don't forget to create a repository on Github with the app you are trying to deploy.
Push the changes you've made with
git add -A
git commit -m "Database Settings ready"
git push origin
Now in the heroku website, go to the Deploy
tab and click in Connect with github.
You will be redirected, to Github in order to authorize access to your repos.
Now connect the repo you want to deploy
And finally in Manual deploy, click on Deploy branch
.
Wait for it, if you get errors ....
Well just read the Heroku docs , or comment below your problems.
Thanks for your time hopefully this tutorial will be useful for you π€.
Follow me in My blog,
to get more awesome tutorials like this one.
Please consider supporting me on Ko-fi you help me a lot to
continue building this tutorials!.
Top comments (3)
Hello there, I receive this error message when trying to migrate the settings.py
decouple.UndefinedValueError: DATABASE_URL not found. Declare it as envvar or define a default value.
any solutions?
Any question, or trouble? Let me know!
hello there, I've posted a comment regarding a problem I have.