The Django app created in tutorials such as Django Girls has been converted to Docker. And I want to deploy the app to Heroku. The tutorial also describes how to deploy Heroku, so basically you can deploy by referring to it. If you don't understand it, you may want to try building, developing, and deploying the environment locally once or twice.
Created an app to be deployed on heroku
requirements.txt
Django==2.2.16
psycopg2
#Below this is the newly added library
dj-database-url
gunicorn
whitenoise==3.0.0
Procfile
web:gunicorn management interface name.wsgi --log-file -
runtime.txt
python-3.6.4
Management interface/local_settings.py
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'postgres',
'USER': 'postgres',
'PASSWORD': 'postgres',
'HOST': 'db',
'PORT': 5432,
}
}
DEBUG = True
Management interface/settings.py
import dj_database_url
...
DEBUG = False
ALLOWED_HOSTS = ['127.0.0.1', '.herokuapp.com']
...
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'postgres',
'USER': 'postgres',
'PASSWORD': 'postgres',
'HOST': 'db',
'PORT': '5432',
}
}
...
db_from_env = dj_database_url.config(conn_max_age=500)
DATABASES['default'].update(db_from_env)
Management interface/wsgi.py
...
# Heroku
from whitenoise.django import DjangoWhiteNoise
application = DjangoWhiteNoise(application)
Deploy to heroku Deploying to heroku can be broadly divided into pushing directly from the command line or auto-deploying Github code, either way to upload the code to heroku.
Set up a production (heroku) database Run the migration on heroku's server and create an admin user This can also be operated from the command line and heroku site
Successful deployment Check if you can deploy properly from the URL
https://tutorial-extensions.djangogirls.org/ja/heroku/ https://devcenter.heroku.com/articles/getting-started-with-python
Recommended Posts