Deploying Django Application on Heroku

Bharat Vora
Coding Blocks
Published in
5 min readApr 20, 2021
Deploy Django application on Heroku with the Postgresql database

As a Python or Django developer, creating a cool Django application and connect it with the Database, it is not the end. You must know how to host that particular application on the internet so that everyone can access it.

Deploying Python application on the internet is a little difficult and confusing. After faced many problems, I’m creating this articles which will guide you to deploy the Django application easily. So, don’t jump to the middle, go with step by step. But first, let’s see some of the top free hosting websites for Python.

Top free hosting websites for Python -

  • PythonAnywhere
  • Amazon AWS
  • OpenShift
  • Heroku

In this article, we’ll see how to deploy the Django application on the Heroku platform with PostgreSQL as a database. Here, I’m used my existing Django ToDo application with Social Authentication.

You can refer my Blog article Django Social Authentication to create social authentication app.

You can check my deployed app on the Heroku platform and for code visit my GitHub Repository.

Let’s start over by creating your awesome Python application.

Let’s start coding

Step-1: Make sure your system has the following packages:

1. Python 3.8.x
2. pipenv

Step-2: Create a Django Application by using my article (mentioned above). If you have already one, then you can also use that.

First, create a virtual environment called shell to isolate the development. You can always change name by whatever you want.

$ pipenv shell

Step-3: Execute the below command to install packages.

$ pip install gunicorn whitenoise dj-database-url psycopg2

Step-4: In your root directory, create a file called Procfile (without extension) and add the below code.

web: gunicorn <#name>.wsgi --log-file -where, <#name> is your application name.  

Step-5: To run the application on the Heroku platform, we have to tell Heroku which dependencies, modules, and packages we used in our app. So, we have to create a requirements.txt file in our root directory and Heroku automatically downloads all the dependencies and packages while building our application.

$ pip freeze > requirements.txt

Step-6: Create a runtime.txt file that includes run-time supported language by Heroku. So, the Heroku knows which programming languages you have used for your application. Check this to know which run-time supported by Heroku.

python-3.8.1

Step-7: Install Git Bash from here if you haven’t already installed it.

After successfully installation, go to your app’s root directory and do
Right click -> Git Bash Here.

Step-8: Now, Create a GitHub repository with a relevant name.

(Do not initialize README.md file)
Copy the code as shown in the below picture and paste it in the Git Bash.

Copy the code and paste it in Git Bash

Step-9: Create .gitignore file by

touch .gitignore

and add the below code.

Step-10: Download & Install Heroku Command Line Interface from here.

Step-11: Now, use your terminal to log in to Heroku. Create a Heroku app.

heroku login 
heroku create #create app with random name
heroku create NameOfApp #create app with custom name

Step-12: Update your app’s settings.py file.

Add your Heroku app’s URL (Generated after step-11) in allowed hosts.

ALLOWED_HOSTS = ['localhost', '127.0.0.1',     'AppName.herokuapp.com']

Set Environment variable for SECRET_KEY.

SECRET_KEY = '' #Your app's secret key
SECRET_KEY = os.environ.get('SECRET_KEY')

Run the command to share your key with Heroku.
heroku config:set SECRET_KEY=secretkey

Check the path of your static files -

STATIC_URL = '/static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')] STATIC_ROOT = os.path.join(BASE_DIR, 'assert')

*If you have written static in DIRS and assert in ROOT like below then Heroku might give you an error —
FileNotFoundError: [Errno 2] No such file or directory: '/app/assert'

To resolve this error, just add assert directory so that Heroku can find it by running —
$ python manage.py collectstatic

In your settings.py make DEBUG = False and Update your INSTALLED_APPS by adding whitenoise.runserver_nostatic and MIDDLEWARE by whitenoise.middleware.WhiteNoiseMiddleware .

  • At top import — import dj_database_url

Add the below code after DATABASE,

db_from_env = dj_database_url.config(conn_max_age=600)                       DATABASES['default'].update(db_from_env)

Step-13: Commit and Save your changes by –
git add . and git commit -m "Initial commit" .

Step-14: Create PostgreSQL database on the Heroku platform.

Create PostgreSQL database on Heroku
heroku addons:create heroku-postgresql:hobby-dev

Now, run heroku pg:info to get database details. Where, Add-on will give you the name of your Heroku Database.

Check out the Heroku PostgreSQL documentation.

Step 15: Disable Collectstatic.

heroku config:set DISABLE_COLLECTSTATIC=1
git push heroku master

After successfully deployed, you can open the app by heroku open .

Check the running application on heroku that I created in this article. You can also refer my code that I pushed on GitHub.

Booom !!! You just successfully created and deployed the django application on Heroku with PostgreSQL as a database.

You’re the great reader. :heart:

Related Articles:

Thank you so much to visit my article. :heart_eyes:

For learning Core Python you can also enroll the following courses of Coding Blocks, which is the best ever Coding Institute in India. I have also enrolled many courses here.

Coding Blocks is the correct place to learn the programming languages and technologies of your choice. You can find here you interest courses like Web Development, Android, Software Training, Programming Courses, Java, Machine Learning, Algorithms, Data Structures, React Native, ReactJS, Artificial Intelligence, Deep Learning, Interview Preparation, Python, Product Management, and Data Science and etc.

You’ll also get the additional discount by using my Campus Ambassador code

50% of on all the course. Use code : CBCA760

--

--

Bharat Vora
Coding Blocks

Python Development | Machine Learning | AWS | Content Writing