Have you ever tried uploading your django app to heroku but felt it was too complicated? If so, in this segment we will look at how you can upload your app to heroku. Don't worry, it is going to be short and to the point.
For this tutorial, I will be assuming you have an app built, so the next step you will be delving into, is on how to deploy your app to heroku.
Keeping that in mind, let's dive in and see how we can setup the app.
If you are already working in a virtualenv
you can easily run to create your requirements.txt
file.
https://gist.github.com/e9b170b07f5ad310b79beb52690c6963
-
Create a file with name
Procfile
(Make sure to not have any extension). TheProcfile
should be in the same directory as yourmanage.py
file. -
Install gunicorn. https://gist.github.com/41a62036d20697ff43de85adf74cc044
-
Make sure to add gunicorn to your
requirements.txt
file. -
Add the following line to your
Procfile
. The app name is basically the folder name where you have yourwsgi.py
file. https://gist.github.com/61923a86ba6b6e7cc24e3c31b40699f1
- Next we will be installing a package called
django-heroku
, after installing make sure to add it on therequirements.txt
file.
https://gist.github.com/633fc00803165a92f7f92e9ee4fa9b19
- Now go to your
settings.py
and import it on top. https://gist.github.com/be5e0fdbfc2a69c211365242e99800b2
and paste this at the bottom of the file or else you are going to get a KeyError
.
https://gist.github.com/2bb602237854ef4303d4fc31a2c61d08
- Now to setup the static assets. https://gist.github.com/d6f83409d4b0e7040b6bcf0a402b88ef
This will basically tell django where to look for the static files and which folder to look for when collectstatic
is run.
-
Install WhiteNoise and update your
requirements.txt
file. https://gist.github.com/841ebbbb879e16c6964e040725073efd -
Next, install WhiteNoise into your Django application. This is done in settings.py‘s middleware section (at the top): https://gist.github.com/07e35679d735f697d3387123791f08ce
-
Finally, if you would like gzip functionality enabled, also add the following setting to settings.py. https://gist.github.com/8c3f01c67742f3758358d66b18b5623b
We will need to create one more file called runtime.txt
. This will tell heroku which version of python needs to be installed. This step is optional because heroku will use a python version automatically when building but if you want a specific python version you can add it like this.
https://gist.github.com/48222f0fcfd39f27c9492ff8fbc31547
That is it for configuring our app for deployment. Now we go to the heroku cli and see how to upload the app.
If you don't have heroku installed on your machine follow this link
- login to heroku. https://gist.github.com/aafc3f4f36204c18e76d7a70a28d25f4
After successful login we will be able to create our app directly from the terminal.
- Create a heroku app https://gist.github.com/b506f9b0d4de8a3e578310425e784b8e
This will create a heroku app with a random available name. However, if you want to give a name of your choice, just add the name after heroku create
. Make sure the name is unique and available.
-
Now add all the files to git and commit. https://gist.github.com/43dcaf8548b8e535a63b87313c5c4e77
-
Push all the files and build. https://gist.github.com/9ce0bd88c8157dbc98ef5d7253d3f0a5
Once done this will deploy your app. Once deployed, we will need to migrate our database.
https://gist.github.com/2eb8994a8f9a7892980ed3b56c9eabd2
This will give us a quick terminal to control our app. Here you can run all your django commands.
https://gist.github.com/3649f1235e8287ff84feb03913094e7a
- This will migrate all our files to the database.
That is all for now. If you have followed all the steps above you should have an app which is now running on heroku. If you have come across any issues please leave a comment or knock-me. I will try to help fix them.
Best of luck. Happy coding :D