How to Deploy a Django Application on Heroku?

Abhinav Jain
4 min readAug 22, 2022

This article is a step-by-step guide which will help you host your Django Applications on Heroku without encountering frustrating errors like “Error 500” or “Internal Server Error” or “Build Failed” or “Push Rejected”.

Fig. Heroku X Django

Let’s get started:

1. Changing “settings.py” & “urls.py” :

In settings.py, edit or change your “MIDDLEWARE” to make it look like this :

MIDDLEWARE = [
‘django.middleware.security.SecurityMiddleware’,
‘whitenoise.middleware.WhiteNoiseMiddleware’,
‘django.contrib.sessions.middleware.SessionMiddleware’,
‘django.middleware.common.CommonMiddleware’,
‘django.middleware.csrf.CsrfViewMiddleware’,
‘django.contrib.auth.middleware.AuthenticationMiddleware’,
‘django.contrib.messages.middleware.MessageMiddleware’,
‘django.middleware.clickjacking.XFrameOptionsMiddleware’,
]

And in urls.py, add the following to “urlpatterns” :

re_path(r'^media/(?P<path>.*)$', serve,{'document_root':       settings.MEDIA_ROOT}), 
re_path(r'^static/(?P<path>.*)$', serve,{'document_root': settings.STATIC_ROOT}),

Also, add the following line at the bottom of your page:

urlpatterns = urlpatterns + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

Note: For adding the aforementioned code successfully, you will have to include the following libraries:

 from django.urls import re_path
from django.conf import settings
from django.conf.urls.static import static
from django.views.static import serve

2. Installing WhiteNoise:

You must have noticed something called “WhiteNoiseMiddleware” in the changes made in settings.py file. Let us understand this in some depth.

Fig. How a web server communicates with applications and static files.

Need to use WhiteNoise :

You can’t host web applications on their own. You require additional files — called static files — for your applications to function properly. These files may be media files, Javascript or CSS among many others. Generally nginx or Amazon S3 is used to store your app’s data in their cloud storage to be retrieved anytime.

WhiteNoise helps you build your app as a single unit of static files to be deployed anywhere on its own without the need for such external servers.

How to install WhiteNoise? :

Write the following code in you terminal to install the latest version of WhiteNoise. You can also install it by downloading its files from here.

pip install whitenoise

3. Adding “Procfile” and “requirements.txt”:

Make a new file named “Procfile” (with a capital “P”, without any extension to avoid all possible errors) in the same directory as the “manage.py” file. This will tell heroku how to run your app. Paste the following code in your Procfile:

web: gunicorn {YOUR_APP_NAME}.wsgi — log-file -

Note: Check if you have Gunicorn installed in your project environment. If not, run the following command or click here to download files:

pip install gunicorn

For deploying your app on heroku, you need a requirements.txt file. To automatically create a requirements.txt file in your directory, write the following code in your terminal:

pip freeze > requirements.txt

This command will add all the dependencies in your app’s environment to requirements.txt file.

4. Adding Your Project to its GitHub Repository:

Go to your GitHub account and click on the “+” button at the top right corner. Then click on “New Repository” and fill all the relevant details about your project. Once created, click on the green “Code” button and copy the HTTPS link. After that, type the following command in your terminal:

 git init

git add .

git commit -m “Initial Commit”

git push {GITHUB_HTTPS_LINK}

This will push all of your project files in your GitHub repository.

5. Avoiding Collectstatic Error on Heroku:

This is a prophylaxis action to avoid any future error on heroku. Follow these simple steps to disable the “collectstatic” during a deploy:

• Install Heroku CLI:

npm install -g heroku

• Login to your Heroku account:

heroku login

• Disable Collectstatic:

heroku config:set DISABLE_COLLECTSTATIC=1

6. Deploy on Heroku:

● Go to your Heroku Dashboard and click on “New”.

● Fill relevant details like the name of your app and choosing a region.

● Go to Settings and click on “Add a Buildpack” and choose Python.

● Go to Deploy -> Deployment Method and click on GitHub.

● Search the name of your repository and connect it.

● Click on “Open App” at the top right corner and copy the URL of the page opened.

● Go to settings.py in your project folder and paste the copied URL in the “ALLOWED_HOSTS”. It should look something like this :

ALLOWED_HOSTS = [‘127.0.0.1’,’{YOUR_APP_URL}’]

Note: Make sure you remove “https://” and any additional “/”s from your URL.

● Write the following code in your terminal one last time:

 git add .

git commit -m “Initial Commit”

git push {GITHUB_HTTPS_LINK}

● Go back to Heroku Dashboard -> Deploy Settings and in “Manual Deploy” choose the branch of the repository you have pushed your files in (generally “master” branch).

● Finally click on “Deploy Branch”.

Once deployed, you can again click on “Open App” to see your deployed application. Keep exploring and happy developing!

--

--