A installation guide for getting Django setup on Heroku
-
Sign up for Heroku Non-Affiliate
-
Download & Install Heroku Command Line Interface
-
Open Terminal or Command Prompt (Windows users)
-
Authenticate with Heroku:
$ heroku login Enter your Heroku credentials. Email: <your-heroku-account-email> Password (typing will be hidden): Logged in as <your-heroku-account-email>
-
Install Python: Python.org or Mac watch here Linux watch here Windows watch here
Start Django Project Locally here ensure you have the following done:
cd ~/Dev/cfehome/src/
pip install gunicorn dj-database-url psycopg2
pip freeze > requirements.txt
-
Initialize Git in the root of your Django Project (where
manage.py
is)# cd /path/to/your/project cd ~/Dev/cfehome/src/ git init
-
Create
.gitignore
file with the exact contents of this .gitignore file. Put at the same path as where you did thegit init
-
Run
git status
result should be:On branch master Initial commit Untracked files: (use "git add <file>..." to include in what will be committed) .gitignore cfehome/ db.sqlite3 manage.py requirements.txt nothing added to commit but untracked files present (use "git add" to track)
-
First Commit
git add --all git commit -m "Intial Commit"
-
Navigate to the root of your Django Project (where
manage.py
is):cd ~/Dev/cfehome/src/
-
Create Procfile with the contents:
web: gunicorn cfehome.wsgi --log-file -
-
Call
git status
, result being:(cfehome) $ git status On branch master Untracked files: (use "git add <file>..." to include in what will be committed) Procfile nothing added to commit but untracked files present (use "git add" to track) (cfehome) $
-
Add Procfile to Git
git add . git commit -m "Added Procfile"
-
Create Heroku App: Use
heroku create <your-app-name>
or justheroku create
, such as:heroku create cfehome
Unsuccessful Result should be:
Creating ⬢ cfehome... ! ▸ Name is already taken
Successful Result should be:
(cfehome) $ heroku create cfehome Creating ⬢ cfehome... done https://cfehome.herokuapp.com/ | https://git.heroku.com/cfehome.git
-
Test Heroku Locally
heroku local web
-
Create
runtime.txt
file for Python Version: In Django Project Root (where manage.py is), create a fileruntime.txt
with the contents:python-3.5.2
Add to git
git add runtime.txt git commit -m "Added runtime file"
-
Update
settings.py
:-
Change
DEBUG = TRUE
toDEBUG = FALSE
-
Add
cfehome.herokuapp.com
toALLOWED_HOSTS
:ALLOWED_HOSTS = ['cfehome.herokuapp.com']
-
-
Create
heroku
Live Database:heroku addons:create heroku-postgresql:hobby-dev
-
Configure Live Database on
settings.py
:# keep this DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), } } # add this import dj_database_url db_from_env = dj_database_url.config() DATABASES['default'].update(db_from_env)
-
Commit:
git add --all git commit -m "Update Django for database"
We suggest using Amazon Web Service S3 for static files in general. However, if you want to use Heroku fr static files, do the following:
-
Install whitenoise and add to
requirements.txt
:pip install whitenoise pip freeze > requirements.txt
-
Update our Production Django Settings file created above called
production.py
(also inlocal.py
&base.py
):MIDDLEWARE = [ 'django.middleware.security.SecurityMiddleware', 'whitenoise.middleware.WhiteNoiseMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', ] # Simplified static file serving. # https://warehouse.python.org/project/whitenoise/ STATIC_URL = '/static/' STATICFILES_DIRS = ( os.path.join(BASE_DIR, "static"), ) STATIC_ROOT = os.path.join(BASE_DIR, "live-static-files", "static-root") STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage' #STATIC_ROOT = "/home/cfedeploy/webapps/cfehome_static_root/" MEDIA_URL = "/media/" MEDIA_ROOT = os.path.join(BASE_DIR, "live-static-files", "media-root")
-
Using Amazon Web Service S3 for static files?
Ensure you do disable collecstatic from running everytime you push to Heroku (which causes errors). Re-enable after you setup S3 in your Django Project.
#disable collectstatic heroku config:set DISABLE_COLLECTSTATIC=1 #enable collectstatic (if needed) heroku config:set DISABLE_COLLECTSTATIC=0
-
Run
collectstatic
locally:python manage.py collectstatic
-
Commit:
git add --all git commit -m "Update Django for whitenoise static"
In your settings file(s) (local.py
, base.py
, production.py
) put in the following:
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = 'youremail@gmail.com' #my gmail username
EMAIL_HOST_PASSWORD = 'yourpassword' #my gmail password
EMAIL_PORT = 587
EMAIL_USE_TLS = True
DEFAULT_FROM_EMAIL = "Justin <hungrypy@gmail.com>"
ADMINS = [('Justin', EMAIL_HOST_USER)]
MANAGERS = ADMINS
If email is faiiling, then go to the folloing locations to unlock your gmail address:
-
heroku domains Heroku Docs:
heroku domains heroku domains:add www.sporproject.com
-
Setup DNS for your Domain:
Type Host/name Answer TTL CNAME www.yourdomain.com cfehome.herokuapp.com 300 CNAME blog.yourdomain.com cfehome.herokuapp.com 300 -
Update
production.py
:ALLOWED_HOSTS = ['cfehome.herokuapp.com', 'www.yourdomain.com']
-
Commit:
git add --all git commit -m "Update Django for custom domain name"
-
After all
git
commits, run:git push heroku master
-
Useful Django commands on Heroku:
heroku bash
opens the shell to do:python manage.py migrate python manage.py createsuperuser python manage.py shell
Shortcut commands:
heroku run python manage.py migrate
heroku run python manage.py createsuperuser
heroku run python manage.py shell
heroku restart
-
When you make changes to Django on your local project:
-
Ensure
makemigrations
is ran prior to deployment:python manage.py makemigrations
-
View local changes:
heroku local web
-
Commit all changes:
git add --all git commit -m "Update something"
-
Push & Migrate:
git push heroku master && heroku run python manage.py migrate
-