Over the last few days, I was looking around the web for a tutorial 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==188.8.131.52 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 "[email protected]"
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.
Welcome to the future..! In this article, we will be dealing with how to learn Machine Learning. We know that humans can learn a lot from their past experiences and that machines follow...
Data structures and Algorithms (DSA) is a term most programmers/Computer science students approach with great dismay. DSA is tricky with its concepts but the power of its...