[PYTHON] How to deploy a Django app on heroku in just 5 minutes

I wrote the article by referring to the official document below. https://devcenter.heroku.com/articles/getting-started-with-django#declare-process-types-with-procfile

Work environment

Prerequisites

  1. Have a heroku account
  2. heroku toolbelt is installed
  3. virtualenv is installed

Introducing PostgreSQL

Installation

$ brew install postgresql

Pass the path (~ / .bash_profile)

export PATH=/usr/local/Cellar/postgresql/9.4.1/bin:"$PATH"

Reboot the terminal

Build a Django environment

** Create and move application management directory **

$ mkdir hellodjango && cd hellodjango

** Build a virtual environment with venv and start it **

$ virtualenv venv
$ source venv/bin/activate

** Download the complete Django-related library **

$ pip install django-toolbelt

** Django project creation **

$ django-admin.py startproject hellodjango .

** Create a Procfile and describe the following contents **

web: gunicorn hellodjango.wsgi --log-file -

** Start local server **

$ foreman start
21:21:26 web.1  | started with pid 5513
21:21:26 web.1  | [2015-05-10 21:21:26 +0900] [5513] [INFO] Starting gunicorn 19.3.0
21:21:26 web.1  | [2015-05-10 21:21:26 +0900] [5513] [INFO] Listening at: http://0.0.0.0:5000 (5513)
21:21:26 web.1  | [2015-05-10 21:21:26 +0900] [5513] [INFO] Using worker: sync
21:21:26 web.1  | [2015-05-10 21:21:26 +0900] [5516] [INFO] Booting worker with pid: 5516

OK if you can access at http://0.0.0.0:5000

Create requirements.txt file representing pip dependencies

$ pip freeze > requirements.txt

Django settings

** Add the following content to the end of settings.py **

# Parse database configuration from $DATABASE_URL
import dj_database_url
DATABASES['default'] =  dj_database_url.config()

# Honor the 'X-Forwarded-Proto' header for request.is_secure()
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')

# Allow all host headers
ALLOWED_HOSTS = ['*']

# Static asset configuration
import os
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
STATIC_ROOT = 'staticfiles'
STATIC_URL = '/static/'

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
)

** Rewrite wsgi.py with the following contents **

import os
from django.core.wsgi import get_wsgi_application
from dj_static import Cling

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "hellodjango.settings")

application = Cling(get_wsgi_application())

git version control

** Create a .gitignore file and add the following contents **

venv
*.pyc
staticfiles

** Add to repository **

$ git init
$ git add .
$ git commit -m "my django app"

Deploy to heroku

** Create repository on heroku **

$ heroku create

** Deploy application to heroku **

$ git push heroku master

** Set the number of dyno **

$ heroku ps:scale web=1

** Check from browser **

$ heroku open

OK if the following screen appears!

Welcome_to_Django.png

Recommended Posts

How to deploy a Django app on heroku in just 5 minutes
How to deploy a Django application on Alibaba Cloud
How to deploy a web app made with Flask to Heroku
Deploy the Django app on Heroku [Part 2]
Deploy the Django app on Heroku [Part 1]
How to develop a cart app with Django
How to create a Rest Api in Django
I want to upload a Django app to heroku
Deploy a Python 3.6 / Django / Postgres web app on Azure
A memorandum on how to use keras.preprocessing.image in Keras
Deploy a Django application on Google App Engine (Python3)
Deploy masonite app on Heroku 2020
How to build a Django (python) environment on docker
How to use Django on Google App Engine / Python
Deploy a Django app made with PTVS on Azure
Deploy django project to heroku
How to run Django on IIS on a Windows server
How to reference static files in a Django project
Deploy a web app created with Streamlit to 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
A note on how to load a virtual environment in PyCharm
How to generate a query using the IN operator in Django
Deploy your Django application on Heroku
How to deploy django-compressor on Windows
Implement a Django app on Hy
How to reflect CSS in Django
Deploy Flask app on heroku (bitterly)
Deploy the Flask app on Heroku
Deploy the Flask app on heroku
Deploy Django in 3 minutes using docker-compose
How to deploy a web application on Alibaba Cloud as a freelancer
How to delete expired sessions in Django
How to get a stacktrace in python
How to test on a Django-authenticated page
How to do Server-Sent Events in Django
How to convert DateTimeField format in Django
Until you deploy a SpringBoot project in Gradle on App Engine Flexible
Build your Django app on Docker and deploy it to AWS Fargate
How to get a namespaced view name from a URL (path_info) in Django
Django Heroku Deploy 1
Django Heroku Deploy 2
How to clear tuples in a list (Python)
How to implement Rails helper-like functionality in Django
How to embed a variable in a python string
Publish django project developed in Cloud9 on heroku
How to reflect ImageField in Django + Docker (pillow)
How to run some script regularly in Django
Enable Django https in just a few lines
How to deploy a Streamlit application to GCP (GAE)
Build a Django environment with Vagrant in 5 minutes
How to implement a gradient picker in Houdini
How to notify a Discord channel in Python
How to live a decent life on 2017 Windows
How to create a multi-platform app with kivy
[Python] How to draw a histogram in Matplotlib
Until you create a new app in Django
How to write a named tuple document in 2020
How to count numbers in a specific range
How to read a file in a different directory