Deploy Django Apps on Heroku — Beginner Version (foolproof)
I do care about sharing my projects with others because feedback is important to me.
First, you have to create an app on Heroku, go to settings add Python buildpack.
Create your project and run the server on localhost
Start a command interface such as git bash, cmd, or powershell. Run the following lines.
django-admin startproject testproject cd testprojectcode .
Open the terminal in your IDE. I am using VSCode and the ‘code .’ command opens up IDE, if it is not working with you, simply open the project with your choice of IDE.
If you are using VSCode Command Prompt always works the best for me so I recommend you to use Command Prompt too.
virtualenv venv.\venv\Scripts\activatepip install djangopython manage.py runserver
by copy+pasting these lines, you must have a running server on your local and your working project tree must look like this
After this step, you can either stop the server by CTRL+C or start a new terminal. If you choose to start a new terminal, DO NOT FORGET to activate your virtual environment by running the command below.
.\venv\Scripts\activate
Now let’s install the last module for this project
pip install gunicornpip freeze > requirements.txt
Gitignore
That’s all we will install for this project. Now we create a .gitignore file.
touch .gitignore
Most of the time, a .gitignore template for Django apps is used. You can find it here. We will simply copy and paste the template to the .gitignore file.
If you have named your virtual environment differently, make sure to add it to gitignore.
Procfile
Procfile is essential for Heroku deployments. First we create a file named Procfile without any extension (important)
touch Procfile
and then add this line inside the file.
web: gunicorn testproject.wsgi
As you may notice, the name before the .wsgi must be your project name. I once forgot to fix it and it cost hours to me, so be careful about that.
Settings
First, go to the settings.py file inside your project and add localhost and Heroku host address to ALLOWED_HOSTS. You can find your Heroku Domain name under the setting tab of your Heroku App.
ALLOWED_HOSTS = [‘127.0.0.1’, ‘mydjtestproject.herokuapp.com’]
Second,
import os
on top of the seetings.py and add this line above STATIC_URL.
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
Where you put this line is not that important but it is good to keep it there for semantic purposes.
Heroku
Finally go to your terminal make sure you have Heroku CLI setup by running
heroku
if you see the list of commands, it means Heroku CLI is successfully set up. If not, go here to set it up.
As you may have noticed, we have not initialized a git repo yet. Now by following the steps under the Heroku app deploy tab we will first login to Heroku and create a repo linking it to our Heroku app.
heroku login
after running this command, you will be directed to your browser to log in.
Run the below command. I again invite you to pay attention that the remote I am connecting to is the project I created so make sure you replace that part of the command with your own Heroku app name.
git initheroku git:remote -a mydjtestproject
Lastly,
git add .git commit -m "Initial commit"git push heroku master
Footnote: You can still link your local repo to a remote Github repo as usual.
We are all done. My server is running on Heroku.
Hopefully, those who followed the steps successfully see their app running. I am aware that this implication has some flaws but I wanted to keep it as simple as possible, the next version will contain how to include environment variables and database settings too.
Peace…