[PYTHON] Memo of deploying Django × Postgresql on Docker to Heroku

Introduction

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.

Premise

Created an app to be deployed on heroku

procedure

  1. Add the required packages to heroku to the list of Python libraries, edit and save

requirements.txt


Django==2.2.16
psycopg2

#Below this is the newly added library
dj-database-url
gunicorn
whitenoise==3.0.0
  1. Create a Procfile in the root directory, edit and save

Procfile


web:gunicorn management interface name.wsgi --log-file -
  1. To let Heroku know which version of Python you want to use.

runtime.txt


python-3.6.4
  1. Create a development environment file, edit and save it This time it is postgresql even in the development environment, so the DATABASE field copies the database settings of its own settings.py. Below is a sample.

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
  1. Edit the configuration file for production environment This also sets the database settings for each person

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)
  1. Edit wsgi.py

Management interface/wsgi.py


...
# Heroku
from whitenoise.django import DjangoWhiteNoise
application = DjangoWhiteNoise(application)
  1. 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.

  2. 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

  3. Successful deployment Check if you can deploy properly from the URL

References

https://tutorial-extensions.djangogirls.org/ja/heroku/ https://devcenter.heroku.com/articles/getting-started-with-python

Recommended Posts

Memo of deploying Django × Postgresql on Docker to Heroku
Change the order of PostgreSQL on Heroku
A memo of a tutorial on running python on heroku
[Heroku] Memo for deploying Python apps using Heroku on Windows [Python]
How to build a Django (python) environment on docker
Miscellaneous notes about deploying the django app on Heroku
Memo of migrating Django's DB from SQLite 3 to MySQL on Docker on Raspberry Pi 4B
Deploy django project to heroku
Don't lose to Ruby! How to run Python (Django) on Heroku
[Django] Memo to create an environment of Django + MySQL + Vue.js [Python]
Freely customize the model. Change the variable name and field name of the model of the Django / Django Rest Framework app ~ How to reflect the deployment on heroku Memo
Completion of docker command on Linux
Deploy your Django application on Heroku
python + django + scikit-learn + mecab (1) on heroku
How to run matplotlib on heroku
python + django + scikit-learn + mecab (2) on heroku
Publish DJango page on heroku: Practice
How to deploy a Django app on heroku in just 5 minutes
Update applications running on Django 1.7 to Django 1.8
Django blog on heroku: login implementation
The story of failing to update "calendar.day_abbr" on the admin screen of django
Migrate Django applications running on Python 2.7 to Python 3.5
How to check the version of Django
Deploy Django api on heroku (personal note)
Solution for errors when deploying to Heroku
[Django] Error encountered when deploying heroku Part 2
Deploy the Django app on Heroku [Part 2]
Deploy the Django app on Heroku [Part 1]
[Django] Trouble encountered when deploying heroku Part 1
A note on enabling PostgreSQL with Django
Launch Flask application with Docker on Heroku
How to run Cython on OSX Memo
I tried to summarize the settings for various databases of Django (MySQL, PostgreSQL)
Django + Docker
How to run a Django application on a Docker container (development and production environment)
[Linux] Review of commands for deploying on AWS
Build Django + NGINX + PostgreSQL development environment with Docker
Publish django project developed in Cloud9 on heroku
What I found by deploying Django on EC2
How to reflect ImageField in Django + Docker (pillow)
How to implement "named_scope" of RubyOnRails with Django
Djnago Memo Set Japanese to filename of Content-Disposition
[Windows] Memo to use Keras on GPU [Tensorflow-GPU]
I want to upload a Django app to heroku
I want to take a screenshot of the site on Docker using any font
To you who are cornered by deploying Django. Django 2.2 deployment full version on Ubuntu 18.04 + Nginx
A memo on how to overcome the difficult problem of capturing FX with AI
Posted the number of new corona positives in Tokyo to Slack (deployed on Heroku)