Over the last few days, I was looking for a tutorial on the web to deploy a Django web application on Heroku. All I could find was pretty long intimidating blog posts, which could only discourage me from starting the process.
I’ve done some research and finally deployed my app on Heroku today. So, I just want to document the steps that I’ve done on this blog post.
I’m using a Windows machine. If you’re using the same, simply follow these steps. If you’re on a Mac or Linux, I hope these same steps will work. But I haven’t tested it though.
Also, I’m using Anaconda as the default Python. If you’re not using Anaconda, that’s fine. You can open your command prompt instead of opening the Anaconda Prompt, which I’m using.
I’m not showing you how to create a Django web app now. You already know that. That’s why you’re reading this article. I’m deploying the blog app that I created earlier.
This is not a detailed tutorial. I don’t explain to you what the purpose of each step is. I want you to complete the deployment as soon as possible. That’s the goal here. You can worry about the reason behind each step later.
So, here are the steps.
You must have a Heroku account. If you don’t have one, why are you wasting your time? Go here and sign up.
You need to have Git installed in your system. If you don’t have, go to this link and install the software.
And the final thing you need is Heroku CLI. Go ahead and install it.
Open Anaconda prompt (or your command prompt if you don’t have Anaconda) and go to your project directory using the cd command. Activate your Django environment if you have one.
Login to Heroku by typing in the following:
We need some prerequisites to deploy a web app to Heroku. First of all, we need a procfile.
Add a Procfile in the project root directory by using the following command:
echo web: gunicorn blog.wsgi --log-file - >Procfile
Note that blog is the name of my project. You need to use the name of your project there.
Add a runtime.txt file in the project root directory and specify the correct Python version.
So, open your project folder manually and create a txt file called ‘runtime’. Open it and paste the following:
This is the version of Python that I’m using.
Install the required packages in the environment by using the following command.
pip install gunicorn dj-database-url whitenoise psycopg2
Add a requirements.txt file using the following command:
pip freeze > requirements.txt
Now, manually go to your project directory and open the requirements.txt file. Remove all other requirements in the file except the following:
dj-database-url==0.4.2 Django==1.11.7 gunicorn==19.7.1 psycopg2==22.214.171.124 pytz==2017.3 whitenoise==3.3.1
Let’s set up the static assets and database configuration. Open up settings.py file and make the following changes, preferably at the bottom of the file.
Add the whitenoise middleware at the top of the middleware list in settings.py file.
Create a Heroku app by using the following command.
heroku create pythonistablogapp
Heroku will let you know if the name is already taken. Give a unique name to your app.
You’ll get a domain name. Add your app domain name to ALLOWED_HOSTS in settings.py.
ALLOWED_HOSTS = ['pythonistablogapp.herokuapp.com']
Now, open the Git CMD software, and go to your project directory using the cd command. Login to Heroku from GIT CMD using heroku login command.
Once you’re logged in, type in the following commands.
heroku git:remote -a pythonistablogapp
git add .
git commit -m "Initial commit"
I got an error message while doing this step like “*** Please tell me who you are.”…
If you get an error like that, verify your email id and user name using the following commands and do the commit again.
git config --global user.email "firstname.lastname@example.org"
git config --global user.name "Your Name"
git commit -m "Initial commit"
Push the project to the remote repository using the following command:
git push heroku master
Migrate the database.
heroku run python manage.py migrate
Open the URL and you’ll see the app running live. If you want to add anything to your app, go to the URL/admin to access your backend database.
That’s it. Class dispersed.
7 thoughts on “How to Deploy Django Web Apps on Heroku for Free”
Thanks for the article!
While executing “git push heroku master” command, I am getting the following error:
Error while running ‘$ python manage.py collectstatic –noinput’.
remote: See traceback above for details.
remote: You may need to update application code to resolve this error.
remote: Or, you can disable collectstatic for this application:
remote: $ heroku config:set DISABLE_COLLECTSTATIC=1
And while executing “heroku config:set DISABLE_COLLECTSTATIC=1” command, I am getting the following error:
Error: Multiple apps in git remotes
» Usage: –remote myheroku
» or: –app nstodoapp
» Your local git repository has more than 1 app referenced in git remotes.
» Because of this, we can’t determine which app you want to run this command against.
» Specify the app you want with –app or –remote.
» Heroku remotes in repo:
» nstodoapp (heroku)
» nstodoapp (myheroku)
» nstodoapp (staging)
Could you please suggest what exactly needs to be done?
App is working
Cheers Nidhisha! Seems like you’re a pretty good debugger????.
very much helpful post
How can I deploy mysql databases django project to heroku ..
And thanks for the tutorial it really helped….
Cheers! Need to take a look at the MySQL deployment. Maybe, I’ll try to create an article for that in the future.