Premise
Write the procedure to deploy to Heroku assuming the same structure as Base application.
[Directory structure](https://suehiro24.esa.io/posts/11#%E3%83%87%E3%82%A3%E3%83%AC%E3%82%AF%E3%83%88 % E3% 83% AA% E6% A7% 8B% E6% 88% 90)
What you are doing at baseline
Separation of setting.py by environment
Development-> root / project-dir / settings / local.py
Production-> root / project-dir / settings / production.py
Environment variable management with .env * python-dotenv
Policy
Do not install django-toolbelt, install necessary libraries at any time
Environment
terminal@django application root directory
heroku login
# Create app
# Memo:Add Remote repository(following)Will also execute
# git remote add heroku [email protected]:{app-name}.git
heroku create {app-name}
.env
root/.env
SECRET_KEY = ooooooooooooooooooooooooooooooooo
DJANGO_SETTINGS_MODULE = fitmod.settings.local
install ”heroku-config”
terminal
# .install plugin for env
heroku plugins:install heroku-config
#Reflected in Heroku environment variables
heroku config:push
terminal
heroku config:set DJANGO_SETTINGS_MODULE = {app-name}.settings.{settings-file name for product}
Procfile
terminal
install gunicorn
web: python manage.py collectstatic --noinput ; gunicorn note_to_self.wsgi --log-file -
heroku config: set DISABLE_COLLECTSTATIC = 1
, k.runtime.txt
root/runtime.txt
python-{python version}
# Ex.) python-3.8.0
wsgi.py
terminal
pip install dj-static
root/project/wsgi.py
import os
from django.core.wsgi import get_wsgi_application
from dj_static import Cling #add to
from dotenv import load_dotenv
load_dotenv('.env')
DJANGO_SETTINGS_MODULE = os.getenv("DJANGO_SETTINGS_MODULE")
os.environ.setdefault('DJANGO_SETTINGS_MODULE', DJANGO_SETTINGS_MODULE)
application = Cling(get_wsgi_application()) # Cling()Add
install
#DB for Heroku PostgreSQL-Installation of the library that handles URLs.
python -m pip install dj_database_url psycopg2
# PostgrSQL Add-on installation
heroku addons:create heroku-postgresql:hobby-dev
usage
settings/production.py
#
import dj_database_url
# Setting PostgreSQL To Heroku
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'fitmod',
'USER': 'name',
'PASSWORD': '',
'HOST': 'localhost',
'PORT': '',
}
}
#Bottom
db_from_env = dj_database_url.config(conn_max_age=500)
DATABASES['default'].update(db_from_env)
requirements.txt
terminal
pip freeze > requirements.txt
tarminal
# Deploy
git push heroku master
migrate, createsuperuser
terminal
heroku run python manage.py migrate
heroku run python manage.py createsuperuser
requirements.txt
asgiref==3.2.7
dj-database-url==0.5.0
dj-static==0.0.6
Django==3.0.5
gunicorn==20.0.4
psycopg2==2.8.5
python-dotenv==0.13.0
pytz==2020.1
sqlparse==0.3.1
static3==0.7.0
Recommended Posts