Hosting Service

01 Oct 2018 | MAK Azad

How to deploy django app in heroku

Category : DJango | Tag : Python | Heroku | Github | Deployment |

In local environment:
1. Install dj-database-url, gunicorn, whitenoise,python-dotenv, psycopg2 
(cnvenv) E:\Development\Python\codernotes\cnprj> pip install dj-database-url gunicorn whitenoise
python-dotenv psycopg2

2. Create a requirements.txt file 
(cnvenv) E:\Development\Python\codernotes\cnprj> pip freeze > requirements.txt

3. Create Procfile
In project root directory(...codernotes\cnprj) create a file named Procfile. it will be as it is, no file extension allowed
and name will be capitalized. Add the following line in the Procfile:

web: gunicorn cnprj.wsgi --log-level=info --log-file -

here cnprj is my project name. cnprj.wsgi name can be any name like xyz.wsgi

4. Create runtime.txt file
Create a file named runtime.txt in project root directory(...codernotes\cnprj)  and add the following line.

python-3.6.5

5. Create  a .env file to project root directory(...codernotes\cnprj)  and add the database URL:

DATABASE_URL=sqlite:///db.sqlite3

Make sure .env is added to .gitignore

6. Create a .gitignore file
in project root directory(...codernotes\cnprj\) create a file named .gitignore, the . (dot) is important in file name and no 
file extenssion is allowed. Add the following file/directory name into .gitignore file 

*.pyc
*~
__pycache__
cnvenv
db.sqlite3
.DS_Store
.env

7. Changes in settings.py (...codernotes\cnprj\cnprj\settings.py) file

# Add these two lines in Import Section
import dj_database_url
import dotenv

DEBUG = False
ALLOWED_HOSTS =['127.0.0.1', 'codernote.azad.space','codernote.herokuapp.com']

# load environment variables from .env
dotenv_file = os.path.join(BASE_DIR, ".env")
if os.path.isfile(dotenv_file):
    dotenv.load_dotenv(dotenv_file)

# In Database Section Remove development setup for database and add the following 2 lines
DATABASES = {}
DATABASES['default'] = dj_database_url.config(conn_max_age=600)

# Static Files for WHITENOISE
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATIC_URL = '/static/'
#STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static'),]
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'

#Midleware for WHITENOISE
In MIDDLEWARE section add the followin line just after first line ('django.middleware.security.SecurityMiddleware',)

'whitenoise.middleware.WhiteNoiseMiddleware',

8. Create a folder in project base directory(...codernotes\cnprj\) named static and create a blank css file in that directory

End of Local Changes

9. Installing Heroku CLI(Command Line Interface)
Download the CLI from here: https://devcenter.heroku.com/articles/heroku-cli
During installation on Windows choose "Custom Installation" .  In the list of components that shows up after that  check the
checkbox in front of "Git and SSH". After installation set environment PATH variable 
setx PATH "%PATH%;C:\Program Files\Git\bin"
Restart the command prompt then activate virtualenv!

10. Login to heroku from CLI
(cnvenv) E:\Development\Python\codernotes\cnprj>heroku login
heroku: Enter your login credentials
Email [arshintechbd@gmail.com]: mak.azad.79@gmail.com
Password: *********
Logged in as mak.azad.79@gmail.com


11. Deploy to heroku
     1.	C:\Users\azad> e:
     2.	E:\ cd E:\Development\Python\codernotes\cnprj
     3.	E:\Development\Python\codernotes\cnprj> heroku login
     4.	E:\Development\Python\codernotes\cnprj>git init
     5.	E:\Development\Python\codernotes\cnprj> heroku create codernote
     6.	E:\Development\Python\codernotes\cnprj>heroku git:remote -a codernote
     7.	E:\Development\Python\codernotes\cnprj> heroku buildpacks:set heroku/python
     8.	E:\Development\Python\codernotes\cnprj> heroku config:set DISABLE_COLLECTSTATIC=1
     9.	E:\Development\Python\codernotes\cnprj>heroku addons:create heroku-postgresql:hobby-dev -a codernote 
   10.	E:\Development\Python\codernotes\cnprj> heroku config -a codernote
[Optional] E:\Development\Python\codernotes\cnprj>heroku pg
    11.	E:\Development\Python\codernotes\cnprj> git add .
    12.	E:\Development\Python\codernotes\cnprj> git commit -am "Clean first commit"
    13.	E:\Development\Python\codernotes\cnprj> git push heroku master
    14.	E:\Development\Python\codernotes\cnprj>heroku run python manage.py makemigrations
    15.	E:\Development\Python\codernotes\cnprj> heroku run python manage.py migrate
    16.	E:\Development\Python\codernotes\cnprj>heroku run python manage.py createsuperuser
    17.	E:\Development\Python\codernotes\cnprj>heroku ps:scale web=1
    18. E:\Development\Python\codernotes\cnprj> heroku open

Some others Heroku Command


echo "web: gunicorn blog:app" >> Procfile

Cloning Heroku app in Local
•	cd E:\Development\Python\
•	E:\Development\Python> heroku login
•	E:\Development\Python> git init
•	E:\Development\Python> heroku git:remote -a codernote
•	E:\Development\Python> heroku git:clone -a codernote

Downloading Heroku app in Local
•  heroku plugins:install heroku-slugs
•  heroku slugs:download -a APP_NAME

View Log $ heroku logs --tail Internal Server Error(500) on Heroku in heroku console python manage.py collectstatic FileNotFoundError: [Errno 2] No such file or directory: '/app/static' Clear the build cache for your app. • $ heroku plugins:install heroku-repo • $ heroku repo:purge_cache -a appname • $ git commit --allow-empty -m "Purge cache" • $ git push heroku master Show command promt history • $ doskey /history 

All post under category DJango


1. How to deploy django app in heroku