[PYTHON] Deploy django project to heroku

Premise

Have a Heroku account toolbelt installed (for those who don't) https://devcenter.heroku.com/articles/heroku-cli git init completed Files that you don't want to give to Heroku, such as sqlite, have already been ignored

Log in to Heroku

Log in at the terminal You will be asked for the email address and password of the account registered on Heroku, so enter it, and if "Logged in as email address" is displayed, login is complete. If you get an error with the heroku login command, use the HEROKU_SSL_VERIFY = disable heroku login command

$ heroku login
Enter your Heroku credentials.
Email: xxxxxx
Password (typing will be hidden):
Logged in as xxxxxx

Then run the create command to create a Heroku server In your browser, log in to Heroku and you should see the server created. "Xxxxxxx" is optional for Heroku. You can specify the name when creating it, but you can change it even after creating it, so this time we will create it by default

$ heroku create
Creating app... done, ⬢ xxxxxxx
https://xxxxxx.herokuapp.com/ | https://git.heroku.com/xxxxxx.git

Creating files for django projects

When deploying to Heroku, I have to tell you the version of python, so I will create a file for it Create a runtime.txt file directly under the project folder and describe the python version when you created the django project.

runtime.txt


python-3.5.2

Then install the gunicorn and whitenoize packages on your terminal.

$ pip inastall gunicorn, whitenoize

After installation, view and copy the installed packages

$ pip freeze
Django==1.10.4
gunicorn==19.6.0
・ ・ ・
abridgement
・ ・ ・

Create requirements.txt in the same hierarchy as runtime.txt and copy and paste (or copy and paste with the following command)

requirements.txt


Django==1.10.4
gunicorn==19.6.0
・ ・ ・
abridgement
・ ・ ・

or

$ pip freeze > requirements.txt

Also, create a Procfile in the same hierarchy as runtime.txt in the same way.

web:gunicorn project name.wsgi --log-file -

Set the static file path in settings.py

settings.py


STATIC_URL = '/static/'

STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')

Also write at the end of wsgi.py to use whitenoise

wsgi.py


from whitenoise.django import DjangoWhiteNoise
application = DjangoWhiteNoise(application)

Those who are developing with sqlite need to put the DB package, so install it Heroku's DB supports PostgreSQL, so describe the driver in requirements.txt as well.

$ pip install dj-database-url

requirements.txt


・ ・ ・
abridgement
・ ・ ・
dj-database-url==0.4.1
psycopg2==2.6.1

I will import the database installed earlier in settings.py

settings.py


import dj_database_url
db_from_env = dj_database_url.config()
DATABASES['default'].update(db_from_env)

After completing the above operations, you can commit and deploy to Heroku by typing the following command. However, since the DB has not been created, we will migrate it. Then you should be able to see it running on Heroku by doing a "heroku open" to see it in your browser.

$ git push heroku master
$ heroku run python manage.py migrate
$ heroku open

You may optionally create a superuser and log in to the admin screen.

$ heroku run python manage.py createsuperuser

This completes the deployment to Heroku!

* How to log out

$ heroku logout

Recommended Posts

Deploy django project to heroku
Django Heroku Deploy 1
Django Heroku Deploy 2
Shell to create django project
Steps to create a Django project
Deploy your Django application on Heroku
Deploy the Django tutorial to IIS ①
Java --Deploy Spring Boot project to GAE
Deploy Django api on heroku (personal note)
Deploy Django + React from scratch to GKE (3) Create a GCP project
Deploy the Django app on Heroku [Part 2]
Deploy the Django app on Heroku [Part 1]
How to deploy a Django app on heroku in just 5 minutes
[Python] Introduce UIKit3 to your Django project
Publish django project developed in Cloud9 on heroku
[Easy detonation velocity 2nd] Deploy Streamlit to heroku
Deploy to Heroku right away without knowing git.
I want to upload a Django app to heroku
heroku deployment memo (Django)
Django project environment construction
unable to import django
[Django] Rename the project
update django version 1.11.1 to 2.2
Start a Django project
How to deploy a Django application on Alibaba Cloud
Memo of deploying Django × Postgresql on Docker to Heroku
To myself as a Django beginner (1) --Create a project app--
How to reference static files in a Django project
Deploy a web app created with Streamlit to Heroku
How to deploy a web app made with Flask to Heroku
Introduction to Python Django (2) Win
Don't lose to Ruby! How to run Python (Django) on Heroku
Deploy masonite app on Heroku 2020
How to deploy the easiest python textbook pybot on Heroku
Deploy Django + React from scratch to GKE (1) Backend development --Nginx + Django
How to do the initial setup from Django project creation
Convert Scratch project to Python
Deploy Django + React from scratch to GKE: Table of Contents
Pass text to Django genericview
django project development environment construction
Steps to deploy EMLauncher on CentOS 8
[Django] How to test Form [TDD]
Django1.11.1 Image uploader Easy to stumble
Deploy script to jboss using fabric
Errors related to memcached in django
Register your Django application in your project
Deploy a Django application with Docker
Common html to rent with Django
How to deploy django-compressor on Windows
python + django + scikit-learn + mecab (1) on heroku
How to run matplotlib on heroku
How to use Django3.x + heroku + cloudinary
python + django + scikit-learn + mecab (2) on heroku
Publish DJango page on heroku: Practice
How to reflect CSS in Django
Deploy Flask app on heroku (bitterly)
How to get started with Django
Run python3 Django1.9 with mod_wsgi (deploy)
Deploy the Flask app on Heroku
Project Euler 4 Attempt to speed up
[Django] Command to output QuerySet to csv