Deploy the Django app on Heroku [Part 1]

Introduction

Record as a reminder when running the Django app on Heroku. Part 1 is about displaying the Django app's start page (It Work page) on Heroku.

Next time Deploy the Django app to Heroku [Part 2] ** My environment **

Things necessary

Configuration of the application to be made this time

The application created this time has the following configuration.

Constitution


myProject
├── venv
├── Procfile
├── db.sqlite3
├── manage.py
├── myDjango
│   ├──__pycache__
│   ├── __init__.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
├── requirements.txt
└── runtime.txt

Root directory name: myProject Django project name: myDjango

procedure

Create a Django app

** Create / move project directory **

$ mkdir myProject
$ cd myProject

** Building and starting a virtual environment **

$ virtualenv venv
$ source venv/bin/activate

Build a virtual environment named venv with virtualenv venv. Run the virtual environment venv with source venv / bin / activate. (Venv) will now appear before the terminal username.

(Supplement) When you want to end the execution of the virtual environment, execute deactivate in the terminal.

** Install django-toolbelt **

$ pip install django-toolbelt

By installing django-toolbelt, the following packages will be installed.

** Create a Django project **

$ django-admin.py startproject myDjango ./

Create a Django project called myDjango in your current directory.

** Change language / time zone ** Rewrite the following part of settings.py in the myDjango directory.

myDjango/settings.py


LANGUAGE_CODE = 'ja'

TIME_ZONE = 'Asia/Tokyo'

** Migrate **

$ python manage.py migrate
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying auth.0001_initial... OK
  Applying admin.0001_initial... OK
  Applying admin.0002_logentry_remove_auto_add... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0002_alter_permission_name_max_length... OK
  Applying auth.0003_alter_user_email_max_length... OK
  Applying auth.0004_alter_user_username_opts... OK
  Applying auth.0005_alter_user_last_login_null... OK
  Applying auth.0006_require_contenttypes_0002... OK
  Applying auth.0007_alter_validators_add_error_messages... OK
  Applying auth.0008_alter_user_username_max_length... OK
  Applying sessions.0001_initial... OK

OK if it looks like this To put it simply, migration is a feature that allows you to create a table in a database in Python without writing SQL. Reference: ["Migration" that Rails beginners can easily trip over](https://www.transnet.ne.jp/2015/12/29/rails "Migration" that Rails beginners can easily trip over "colnr /" https://www.transnet. ne.jp/2015/12/29/rails "Migration" colnr / ") that beginners can easily trip over

** Start server **

$ python manage.py runserver
Performing system checks...

System check identified no issues (0 silenced).
July 16, 2017 - 02:53:26
Django version 1.11.3, using settings 'myDjango.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

Execute python manage.py runserver and when the above display is displayed, it's OK By executing this command, you can see the app at http://127.0.0.1:8000/. Press Ctrl + C to exit.

** Check if the app starts locally ** Access http://127.0.0.1:8000/ from your browser. If all goes well, you'll see a page like the one in the image below. はじめてのDjangoページ

Version control with git

** Creating .gitignore ** Create a .gitignore file and write the following:

.gitignore


*.pyc
venv
staticfiles
db.sqlite3

commit

$ git init
$ git add .
$ git commit -m "I created a Django app"

Deploy to Heroku

** Creating a Procfile **

$ echo "web: gunicorn myDjango.wsgi --log-file -" > Procfile

Procfile is a file that teaches Heroku what to do. Reference: Role of Heroku's Procfile

** Creating runtime.txt **

$ echo "python-3.6.1" > runtime.txt

Specify the Python version in runtime.txt.

** Creating requirements.txt **

$ pip freeze > requirements.txt

You can create a file like this

requirements.txt


dj-database-url==0.4.2
dj-static==0.0.6
Django==1.11.3
django-toolbelt==0.0.1
gunicorn==19.7.1
psycopg2==2.7.1
pytz==2017.2
static3==0.7.0

** Make the Django app available on Heroku ** Add the following to the last line of settings.py.

myDjango/settings.py


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

# 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

myDjango/wsgi.py


import os

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

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

application = Cling(get_wsgi_application())

** Creating a Heroku app **

$ heroku create

** Setting Heroku environment variables **

$ heroku config:set DISABLE_COLLECTSTATIC=1

If you do not do this, you will get an error when pushing.

** Push to Heroku **

$ git add .
$ git commit -m "Made it work on Heroku"
$ git push heroku master

Push successful when remote: Verifying deploy ... done. is displayed

** Migrate on Heroku **

$ heroku run python manage.py migrate

If you run heroku run 〇〇, you can execute the command 〇〇 on heroku.

** Check if it works on Heroku **

$ heroku open

You can access the application on Heroku created this time with heroku open. If all goes well, you'll see a page like the one in the image below. Heroku上のはじめてのDjangoページ

Summary

This time, I even ran a Django-made app on Heroku. Next time, we will create a model for the management site.

Next time Deploy the Django app to Heroku [Part 2]

reference

Recommended Posts

Deploy the Django app on Heroku [Part 2]
Deploy the Django app on Heroku [Part 1]
Deploy the Flask app on Heroku
Deploy the Flask app on heroku
Deploy masonite app on Heroku 2020
Miscellaneous notes about deploying the django app on Heroku
Deploy Flask app on heroku (bitterly)
Django Heroku Deploy 1
Django Heroku Deploy 2
Deploy Django api on heroku (personal note)
How to deploy a Django app on heroku in just 5 minutes
Deploy a Python 3.6 / Django / Postgres web app on Azure
Deploy a Django application on Google App Engine (Python3)
Deploy a Django app made with PTVS on Azure
Deploy django project to heroku
How to deploy the easiest python textbook pybot on Heroku
(Failure) Deploy a web app made with Flask on heroku
Django --Overview the tutorial app on Qiita and add features (2)
Run the app with Flask + Heroku
python + django + scikit-learn + mecab (1) on heroku
Implement a Django app on Hy
Detect app releases on the App Store
Try Ajax on the Django page
python + django + scikit-learn + mecab (2) on heroku
Publish DJango page on heroku: Practice
Deploy the Django tutorial to IIS ①
Django blog on heroku: login implementation
[Python + heroku] From the state without Python to displaying something on heroku (Part 1)
[Python + heroku] From the state without Python to displaying something on heroku (Part 2)
[Django] Error encountered when deploying heroku Part 2
[Django] Trouble encountered when deploying heroku Part 1
Change the order of PostgreSQL on Heroku
Heroku deployment of the first Django app that beginners are addicted to
Build your Django app on Docker and deploy it to AWS Fargate
Django begins part 1
Redis on Heroku
Publish django project developed in Cloud9 on heroku
Exclusive release of the django app using ngrok
Django begins part 4
shimehari on heroku
Deploy Django apps on Ubuntu + Nginx + MySQL (Build)
I want to upload a Django app to heroku
When I deployed the Django app to Heroku, I got Module Not Found: <project-name> .wsgi.
Run the flask app on Cloud9 and Apache Httpd
Create a Todo app with the Django REST framework
Publish your Django app on Amazon Linux + Apache + mod_wsgi
I tried python on heroku for the first time
Memo of deploying Django × Postgresql on Docker to Heroku
Deploy an existing app with docker + pyenv-virtualenv + uwsgi + django
Make the model a string on a Django HTML template
Django page released on heroku: Preparation my addictive point
How to use Django on Google App Engine / Python
Django-Overview the tutorial app on Qiita and add features (1)
Install django on python + anaconda and start the server
Deploy a web app created with Streamlit to Heroku
Launch my Django app
heroku deployment memo (Django)
Initialize your Django app
Celery notes on Django
Run Django on PythonAnywhere
Hello World on Django