Deploy a Django app to Heroku
Index:
- Requirements
- Install the Heroku CLI
- Deploying Python and Django Apps on Heroku
- Configure Django apps for Heroku
- Deploy to Heroku
- References
Requirements:
- Git installed
- Python >3.6
- Heroku CLI (see step 1)
1) Install the Heroku CLI
Install the Heroku Command Line Interface (CLI) for your platform from here. Once installed, you can use the heroku
command from your command shell. To log in to the Heroku CLI use:
2) Deploying Python and Django apps on Heroku
Heroku automatically identifies your app as a Python app if any of the following files are present in its root directory:
* Pipfile
* setup.py
* requirements.txt
When you deploy to Heroku, the dependencies you specify are automatically installed. If you’re using Django, the collectstatic
command also runs automatically during the deployment process. For this to work properly make sure you install the Django-Heroku Python package (step 3).
3) Configure Django apps for Heroku
Create a Procfile
(no extension) and add the following content where myproject
is the name of your Django app.
Install gunicorn
:
Install the django-heroku
package
Add the following import statement to the top of settings.py
:
settings.py
:
4) Deploy to Heroku
Using the Heroku CLI lets create our app and the database.
Now lets push our code to Heroku:
Note: If you wish to depoly from a local branch other than master
, e.g. testbranch
then use:
Finally, migrate your Database to the Heroku app:
You should now be able to see your app in the Heroku Dashboard as well as a Dyno web process with the ON
indication.
5) Import local database to Heroku (Optional)
If you are using a postgresql database locally you can easily import it to your newly created Heroku app. First create a backup of your local DB:
To import it to Heroku run: