Skip to content

Instantly share code, notes, and snippets.

@demukeshchouhan
Created September 12, 2017 14:02
Show Gist options
  • Save demukeshchouhan/803d9bd057abc823712c7be7d5e63de1 to your computer and use it in GitHub Desktop.
Save demukeshchouhan/803d9bd057abc823712c7be7d5e63de1 to your computer and use it in GitHub Desktop.
How to Deploy Django Application on Heroku !
# Deploy Django App to Heroku
```sh
pip install dj-database-url gunicorn whitenoise
```
```
sh pip freeze > requirements.txt
```
+ add `psycopg2==2.6.2` at the end in `requirements.txt`)
+ Create Procfile file for Heroku and add:
```sh
web: gunicorn mysite.wsgi
```
+ Create runtime.txt then add python version which you are using (python-3.5.2) make sure `p` is not in Caps.
+ Create `local_settings.py` inside your main app, suppose you have main dir. named `maiProject` then you will have same dir. inside with same name along with `setting.py` file. Add following code.
```sh
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
DEBUG = True
```
+ Add following code inside `settings.py` file
```sh
import dj_database_url
DATABASES['default'] = dj_database_url.config()
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
ALLOWED_HOSTS = ['*']
DEBUG = False
try:
from .local_settings import *
except ImportError:
pass
```
+ Add code inside `wsgi.py`
```sh
from whitenoise.django import DjangoWhiteNoise
application = DjangoWhiteNoise(application)
```
# Create account on Heroku and download tollbelt from https://toolbelt.heroku.com/
+ heroku login
+ create `.gitignore`
+ add following code:
```sh
db.sqlite3
myvenv
__pycache__
local_settings.py
git status
git add -A .
git commit -m "Throwing it to heroku 1 "
heroku create youcanaddnamehereifyouwant
git push heroku master
heroku ps:scale web=1
heroku run python manage.py migrate
heroku run python manage.py createsuperuser
```
# Make sure you have index url setup otherwise you will see an error on page.
+ Now access /admin url if you don't see css please follow below instructions:
+ create static file outside add your css and whatever you need (js, images)
+ Then run `python manage.py collectstatic`
It's gonna create staticfiles dir. for you which gonna include admin folder inside then use
Now go to settings.py file and add following code:
```sh
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
]
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
```
Now push it to heroku again via:
+ `git add .`
+ `git commit -m "changed"`
+ `git push heroku master`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment