How to setup Django project for Heroku Prod Deployment?
Setup the Django Project
Assuming python and pip are alreay installed in your system<br>
pip install pipenvPipenv creates a seperate environment for all our dj projects<br>mkdir <MY_DJANGO_PROJECT> && cd <MY_DJANGO_PROJECT>pipenv shellpipenv install djangocreates a Pipfile and Pipfile.lockdjango-admin startproject <my_dj_project>cd <my_dj_project>python manage.py makemigrationspython manage.py migratepython manage.py runserver [PORT]PORT is optional
Setup the DEV and PROD environment
-
pipenv install django-environ -
touch .envCreate a dotenv file in <my_dj_project> and copy the SECRET_KEY from <my_dj_project>/settings.py.env#Sample .env file SECRET_KEY="=o)p$2&wd@%!x(@5^ujkb^g-di+t8zfk+h6g&6=notcy+7rq=$" DEBUG=True -
Make the following changes to <my_dj_project>/settings.py
settings.pyimport environ env = environ.Env() environ.Env.read_env() # reading .env file # SECURITY WARNING: keep the secret key used in production secret! SECRET_KEY = env.str('SECRET_KEY',default='=o)p$2&wd@%!x(@5^ujkb^g-di+t8zfk+h6g&6=notcy+7rq=$') # SECURITY WARNING: don't run with debug turned on in production! DEBUG = env.bool('DEBUG',default=False) -
Make sure to add the
.envfile in.gitignore
Setup for Heroku Deployment
PART 1 - Configuring the basics
-
Make sure you are currently in <MY_DJANGO_PROJECT>
-
pipenv install gunicorn django_heroku -
touch ProcfileHeroku web applications require aProcfileProcfileweb: gunicorn <my_dj_project>.wsgi -
Make the following changes to <my_dj_project>/settings.py
settings.pyimport django_heroku # at the top of settings.py .... .... .... # Activate Django-Heroku. django_heroku.settings(locals()) # at the bottom of settings.py
PART 2 - Configuring the static assets
-
pip install whitenoiseDjango does not support serving static files in production. However, the fantastic WhiteNoise project can integrate into your Django application, and was designed with exactly this purpose in mind. -
Make the following changes to <my_dj_project>/
settings.py. Make sure to follow the instructions in comments -settings.py# add WhiteNoise to the MIDDLEWARE_CLASSES list, above all # other middleware apart from Django’s SecurityMiddleware MIDDLEWARE = [ # 'django.middleware.security.SecurityMiddleware', 'whitenoise.middleware.WhiteNoiseMiddleware', # ... ] .... .... .... .... BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) ## Add the following static file configurations # Static files (CSS, JavaScript, Images) # https://docs.djangoproject.com/en/1.9/howto/static-files/ STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles') STATIC_URL = '/static/' # Extra places for collectstatic to find static files. STATICFILES_DIRS = ( os.path.join(BASE_DIR, 'static'), ) STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage' -
You can have a look at the entire settings.py.
-
This is important. Deployment in Heroku will fail w/o this
mkdir staticCreate a static folder in <MY_DJANGO_PROJECT>.touch static/.keepA .keep file so that Git commits this folder as well- run
python manage.py collectstatic --noinputto see if the command ran successfully otherwise fix errors.
-
stop the server and re-run it
python manage.py runserver [PORT]to make sure the server is running smoothly
PART 3 - Setup the Heroku environment
Assuming you have heroku account & heroku cli installed in your system
-
heroku login -ilogin to your heroku account -
pip freeze > requirements.txtin <MY_DJANGO_PROJECT> dir -
Create the Heroku app
heroku create <my-heroku-app-name>in <MY_DJANGO_PROJECT> dirgit remote -vto confirm that a remote named heroku has been set for your app$ git remote -v heroku https://git.heroku.com/<my-heroku-app-name>.git (fetch) heroku https://git.heroku.com/<my-heroku-app-name>.git (push)
-
For an existing Heroku app, set the git remote of your poject to the app git
$ heroku git:remote -a <my-heroku-app-name> set git remote heroku to https://git.heroku.com/<my-heroku-app-name>.git -
Set Heroku env varibales
$ heroku config:set SECRET_KEY="=o)p$2&wd@%!x(@5^ujkb^g-di+t8zfk+h6g&6=notcy+7rq=$" $ heroku config:set DEBUG=True # turn this to false later
PART 4 - Deploy the app
- Checklist before deploying -
Procfilerequirements.txtPipfile&Pipfile.lockstaticdir- all configurations committed
git push heroku masterpush the changesheroku logs -tfor viewing logs
Part 5 - Post build tasks
heroku run bashin <MY_DJANGO_PROJECT> dir to access the app shell environment on Herokupython manage.py makemigrationspython manage.py migrate- And you are good to go 😃 👍⚡
Blog
Latest Blog Posts
Beyond the Binary 0️⃣ 1️⃣ - Exploring the Intersection of Code and Communication.
