[PYTHON] Deploy your Django application on Heroku

Overview



Create an app on Heroku

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}  


Using .env on Heroku

.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

Set environment variables in Heroku to specify settings for production

terminal


heroku config:set DJANGO_SETTINGS_MODULE = {app-name}.settings.{settings-file name for product}


Creating the files needed for deployment

Procfile

Install gunicorn

terminal


install gunicorn
Procfile creation
web: python manage.py collectstatic --noinput ; gunicorn note_to_self.wsgi --log-file -

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


Settings when using PostgreSQL

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

Deployment and Django settings

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

Deploy your Django application on Heroku
Django Heroku Deploy 1
Django Heroku Deploy 2
Deploy Django api on heroku (personal note)
Deploy the Django app on Heroku [Part 2]
Deploy the Django app on Heroku [Part 1]
Deploy masonite app on Heroku 2020
Install Django on your Mac
Deploy django project to heroku
Deploy a Django application on Google App Engine (Python3)
Register your Django application in your project
Deploy a Django application with Docker
python + django + scikit-learn + mecab (1) on heroku
python + django + scikit-learn + mecab (2) on heroku
Publish DJango page on heroku: Practice
Deploy Flask app on heroku (bitterly)
Deploy the Flask app on Heroku
Deploy the Flask app on heroku
Deploy a Django application on EC2 with Nginx + Gunicorn + Supervisor
Django blog on heroku: login implementation
How to deploy a Django app on heroku in just 5 minutes
Launch Flask application with Docker on Heroku
Redis on Heroku
shimehari on heroku
Build your Django app on Docker and deploy it to AWS Fargate
Until you publish (deploy) a web application made with bottle on Heroku
Until you publish your Django application (+ MySQL) on AWS EC2 (+ RDS (+ S3))
Publish django project developed in Cloud9 on heroku
Deploy Django apps on Ubuntu + Nginx + MySQL (Build)
Deploy a Python 3.6 / Django / Postgres web app on Azure
heroku deployment memo (Django)
Initialize your Django app
Celery notes on Django
Run Django on PythonAnywhere
Publish your Django app on Amazon Linux + Apache + mod_wsgi
Try running a Django application on an nginx unit
Memo of deploying Django × Postgresql on Docker to Heroku
Django page released on heroku: Preparation my addictive point
Single sign-on to your Django application with AWS SSO
Hello World on Django
Deploy a Django app made with PTVS on Azure
Miscellaneous notes about deploying the django app on Heroku
Don't lose to Ruby! How to run Python (Django) on Heroku
How to deploy the easiest python textbook pybot on Heroku
(Failure) Deploy a web app made with Flask on heroku
Deploy your Go app on Google App Engine with GitHub Actions
To deploy Java application on VPS (Apache / Tomcat installation / linkage)
Deploy Django serverless with Lambda
[Django] Notes on using django-debug-toolbar
Install numba on your Mac
Web application creation with Django
Django environment development on Windows 10
Create your own Django middleware
Rename an existing Django application
Hello World (beginners) on Django
How to deploy a web application on Alibaba Cloud as a freelancer
Launched a web application on AWS with django and changed jobs