Upload django project to Heroku

2019-06-04

let's see how to upload(deploy) django project to Heroku for production web service.

Outline

in this blog, we’ll see how to upload(deploy) django project to Heroku for web service.

this blog is a series. if you want to check other blog posts of the series, see the links below.

also, this blog series source code is opened on Github. you can see full source code via the link below.

Sign Up Heroku

we need to sign up Heroku to use heroku service. signup and signin Heroku to click the link below.

I won’t mention how to sign up and sign in Heroku service because it is a normal process to use the service.

Upload(Deploy) django project to Heroku - 회원 가입

Create Heroku App

we need to create Heroku App for Heroku web service.

after signing-up and signing-in, you can see the (Dashboard) screen like below. click Create new app to create new app.

Upload(Deploy) django project to Heroku - create app

insert an app information to create Heroku app. it’s free so we can make United State and Europe region only. if you’ve done, click Create app butotn.

Upload(Deploy) django project to Heroku - insert app information

Install Heroku CLI

we’ll use Heroku CLI(Command Line Interface) to upload(Deploy) our django project to Heroku. click the link below to download Heroku CLI for each OS. in this blog, I will use macOS.

on macOS, execute the command below to install Heroku CLI.

brew tap heroku/brew && brew install heroku

Login Heroku

for deploying a web service to Heroku, we need to login to Heroku via Heroku CLI. execute the command below to login Heroku service.

heroku login

after executing the Heroku command above, you can see the login screen on the browser. click Log in button.

Upload(Deploy) django project to Heroku - Heroku Login

after login, you can see the login success screen like below,

Upload(Deploy) django project to Heroku - 헤로쿠 로그인 성공

or also you can see the success message on the termial that you executed the heroku command

heroku: Press any key to open up the browser to login or q to exit:
Opening browser to https://cli-auth.heroku.com/auth/browser/f844937e-aaaf-4620-998f-1f938ed3dea7
Logging in... done
Logged in as dev.yakuza@gmail.com

Modify django Project

we’re ready to deploy to Heroku service. however, we need to modify our django project to work on Heroku service. create Procfile to the folder that has manage.py file and modify it like below.

web: gunicorn django_exercise.wsgi --log-file -

in above, you should modify django_exercise.wsgi to your project name([project name].wsgi). we notice guniconr that is python HTTP server(Python WSGI HTTP Server) on Heroku service to Heroku.

execute the command below to install modules to use django on Heroku service.

# source venv/bin/activate
pip install gunicorn dj-database-url psycopg2-binary whitenoise
  • gunicorn: this is python HTTP server(Python WSGI HTTP Server).
  • dj-database-url, psycopg2-binary: Heroku provides postresql and these are modules for django.
  • whitenoise: this module is for django uses static files on Heroku.

after installing, execute the command below to update requirements.txt.

pip freeze > requirements.txt

after updateing, open and modify django_exercise/settings.py like below.

...
import dj_database_url
...
ALLOWED_HOSTS = ['127.0.0.1', '.herokuapp.com']
...
MIDDLEWARE = [
    'whitenoise.middleware.WhiteNoiseMiddleware',
    ...
]
...
db_from_env = dj_database_url.config(conn_max_age=500)
DATABASES['default'].update(db_from_env)

and then, we need to notice our django python version to Heroku. execute the command below to checkou python version.

# source venv/bin/activate
python --version
# Python 3.7.2

and then, create and modify runtime.txt on the folder which has manage.py.

python-3.7.2

lastly, we need to disable django Debug mode for production environment. open and modify django_exercise/settings.py like below.

...
DEBUG = False
...

and, store everything to our git.

git add .
git commit -m 'prepare to deploy'
git push origin master

Deploy(Upload)

we’ve done. let’s deploy django project on Heroku. execute the command below to add Heroku to git remote.

heroku git:remote -a djangoexercise

execute the command below to check Heroku added well.

git remote -v

you can see Heroku added in git remote like below.

heroku  https://git.heroku.com/djangoexercise.git (fetch)
heroku  https://git.heroku.com/djangoexercise.git (push)
origin  https://github.com/dev-yakuza/django_exercise.git (fetch)
origin  https://github.com/dev-yakuza/django_exercise.git (push)

execute the command below to deploy.

git push heroku master

Heroku is based on git for deployment. our django project is already deployed by the command above. now, we need to configure the database by our migration and create new administrator account for django. execute the command below to configure the database.

heroku run python manage.py migrate

execute the command below to make django administrator.

heroku run python manage.py createsuperuser

Check

let’s check our django project deployed on Heroku. execute the command below to open our site on the browser.

heroku open

we can see our django proejct deployed well like below.

Upload(Deploy) django project to Heroku - check heroku deploy

add /admin to URL to go to the admin page.

Upload(Deploy) django project to Heroku - check django admin page

and login with superuser we’ve created above.

Upload(Deploy) django project to Heroku - django admin page

we can see Post Model we’ve made before.

Completed

Done! we’ve seen how to deploy django project to Heroku service. now, we are ready to develop the web service with django! after this blog series, I’ll write about what I use when I develop the service with django.

Buy me a coffeeBuy me a coffee
Posts