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
pip freeze > requirements.txt
If you want to manually write the version of the package just go to pypi website and find the latest version.
It should look something like this:
asgiref==3.3.4 Django==3.2.3 gunicorn==20.1.0 Pillow==8.2.0 pytz==2021.1 sqlparse==0.4.1 django-heroku==0.3.1 whitenoise==5.2.0
- Create a file with name
Procfile(Make sure to not have any extension). The
Procfileshould be in the same directory as your
- Install gunicorn.
pip install gunicorn
Make sure to add gunicorn to your
Add the following line to your
Procfile. The app name is basically the folder name where you have your
web: gunicorn <app_name>.wsgi
- Next we will be installing a package called
django-heroku, after installing make sure to add it on the
pip install django-heroku
- Now go to your
settings.pyand import it on top.
and paste this at the bottom of the file or else you are going to get a
- Now to setup the static assets.
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
pip install whitenoise
- Next, install WhiteNoise into your Django application. This is done in settings.py‘s middleware section (at the top):
MIDDLEWARE_CLASSES = ( # Simplified static file serving. # https://warehouse.python.org/project/whitenoise/ 'whitenoise.middleware.WhiteNoiseMiddleware', ...
- Finally, if you would like gzip functionality enabled, also add the following setting to settings.py.
# Simplified static file serving. # https://warehouse.python.org/project/whitenoise/ STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
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.
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.
After successful login we will be able to create our app directly from the terminal.
- Create a heroku app
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.
If you already have a heroku app and want to add the app as a Git remote, you need to execute
heroku git:remote -a <yourapp>
- Now add all the files to git and commit.
git add . git commit -m "deploy heroku"
- Push all the files and build.
git push heroku master
Once done this will deploy your app. Once deployed, we will need to migrate our database.
heroku run bash
This will give us a quick terminal to control our app. Here you can run all your django commands.
python manage.py migrate
- 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