(For personal reference, this is what worked for me)
I tried a couple ways to deploy a Django blog app to Heroku and finally found a way that worked for me!
I used the django-heroku library. It helps automatically configure DATABASE_URL
, ALLOWED_HOSTS
, WhiteNoise (for static assets), Logging, and Heroku CI for the application so we don’t have to ourselves. For reference, https://github.com/heroku/django-heroku
I also used gunicorn, which is a Python WSGI HTTP Server for UNIX. (I don't install this locally since I'm working in a windows environment but since Heroku needs gunicorn for deployment, I have it as something Heroku itself needs to download)
Here’re the things that I added/steps that I took:
requirements.txt
Let Heroku know what packages it needs to download in a requirements.txt file in the main directory. In here we have Django, django-heroku, and gunicorn
maindirectory/requirements.txt:
Django~=<version number>
gunicorn
django-heroku
Procfile
Set up a Procfile in the main directory (important: it needs to be titled as Procfile, not Procfile.txt!) A Procfile declares what commands should be run by Heroku in order to start up the website.
maindirectory/Procfile:
web: gunicorn <mysite>.wsgi --log-file -
This line means that we're deploying a web application and that it should be started by running gunicorn mysite.wsgi
settings.py
Configure the app using django-heroku somewhere in settings.py
maindirectory//settings.py:
# Configure Django App for Heroku.
import django_heroku
django_heroku.settings(locals())
Install Heroku
*This step can be skipped if it's already done
Install the Heroku CLI from here: https://devcenter.heroku.com/articles/heroku-cli
Authenticate the Heroku account on the computer by running this command:
$ heroku login
Deploy
Now to deploy to Heroku! Create a heroku application on the command line:
$ heroku create <app name>
commit changes and push
$ git add .
$ git commit -m "some message"
$ git push heroku
We’ve deployed your code to Heroku, and specified the process types in the Procfile
(we chose a web
process type earlier). We can now tell Heroku to start this web process
.
To do that, run the following command:
$ heroku ps:scale web=1
We can now visit the app in our browser with heroku open
.
$ heroku open
Top comments (0)