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 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
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!
$ heroku logout
Recommended Posts