Deploy Your Django Project for FREE! (2024)

Deploy Your Django Project for FREE! (3)

In this article, I will go over all the steps required to deploy any Django project on Heroku. Heroku is a really powerful platform for deploying not only Django apps and projects but also apps built on React and Node. In this article, I will explain everything you need to do in order to deploy a Django project.

What is Django?

For those who are new to Python web development, Django is a powerful web development framework. It is a high-level Python web framework that encourages rapid development and clean, pragmatic design. It takes care of much of the hassle of web development and has an easy approach to the routing between web pages. Django is fast, scalable, and highly secure. It’s a framework that makes it easy for the front-end to interact with back-end databases. The best part: Django is free and open-source!

There are multiple articles on medium, and many youtube videos, which explore how to get started with Django. In this article, I will focus mainly on deploying an already built project on Heroku servers.

What is Heroku?

Heroku is a container-based cloud Platform as a Service (PaaS). Developers use Heroku to deploy, manage, and scale modern apps. The platform is elegant, flexible, and easy to use, offering developers the simplest path to getting their apps to market. On Heroku, one can deploy Django apps for free. For your web app, Heroku also lets you choose your own name for your app. Hence you can create custom domain names for your web application and launch it on the internet so that all your friends can see your cool web projects! If you already have a domain name registered, you can add that on Heroku easily by just one step, which I will be explaining in the latter part of this article.

Note: The article from now will be assuming that you have already developed a Django App which is ready to be deployed.

To start with, there are few prerequisites needed before your website goes viral on the internet. There are two pieces of software that need to be installed on your PC to help you deploy the app.

  1. Git — Git is a version control tool that enables you to push and commit new changes to your codebase while preserving the previous versions. It's good practice to use Git for managing your project. To install Git, you can download it from the link provided, based on your OS — https://git-scm.com/downloads
  2. Heroku CLI — Heroku CLI is the Heroku client application, which is used to push your code to the Heroku web server. You will need to download the Heroku CLI and set it up with Git on your PC. This has been explained wonderfully on Heroku CLI website. Here is the link for the same — https://devcenter.heroku.com/articles/heroku-cli
  3. Heroku Login — Another requirement is that you will have to create a Heroku account. This can be made on https://www.heroku.com
  4. Python Virtual Environment — The next requirement is python virtual environment setup. Python virtual environment creates an environment for your project, within which you can install the dependencies which only that project requires. This helps you install only those dependencies on the cloud and save up space. To install Python Virtual environment, just run pip install virtualenv. Here is the link for the same — https://pypi.org/project/virtualenv/

There are few library dependencies which is required for deploying your app. I will cover them as and when needed in this article.

Now we have finished with the prerequisites, it’s time to get started.

Setting your virtual env for an already existing Django project

To start with, first you will have to create the virtual env for your project. I will be creating my virtual env in E drive for example. To create the env, you’ll have to open cmd prompt in E drive and type the following command.

Deploy Your Django Project for FREE! (4)

Myvirtualenv is the name of the virtual env. You can give any name. The next step is to activate this virtual env. This is done by the following commands.

>cd Myvirtualenv
>Scripts\activate Myvirtualenv

Now, the next step is to install all dependencies which are required for your Django project. The main dependency is Django itself, so go ahead and type “pip install Django” in the cmd line and press enter.

Once this is done, I recommend you create a new folder with the name as your Django project’s name in the same drive itself (E drive for example). Within this folder, copy all your project files. It will look something like this —

Deploy Your Django Project for FREE! (5)

“Mywebsite” is the name of my Django project and these are the files inside it. Navigate into this folder through the cmd line and install all the dependencies of your project.

Once all the dependencies are installed, you will need to install some additional dependencies which are mentioned below. These dependencies are used of working with Heroku. Django-Heroku is a library that takes care of creating a PostgreSQL database for you and linking your current database with it, as Heroku only supports PostgreSQL.

pip install django-heroku
pip install gunicorn

Once these are installed within your virtual env, you can try running “python manage.py runserver” to check if your website is running perfectly on your localhost or not.

Exporting all the requirements

The next step is to let Heroku know what all requirements will be required for your project. This is done by a simple command mentioned below-

pip freeze > requirements.txt 

The above script gets all your requirements and dumps it into a requirements.txt file.

Setting up project for Heroku

This is the main step to deploy your code on the server. We need to create a procfile first. The procfile tells Heroku that the project is a web project.

To create a procfile, you simply have to create a text document with the following lines within it. It should be saved as Procfile.file initially, and later renamed to “Procfile”.

web: gunicorn myproject.wsgi

Your Procfile will look something like this —

Deploy Your Django Project for FREE! (6)
Deploy Your Django Project for FREE! (7)

Remember we installed Django-Heroku library? We just need to add two lines related to it in our settings.py, so that Heroku can do the background linking of our database.

You will need to add “import django_heroku” at the beginning of the script in settings.py and add the line “django_heroku.settings(locals())" at the end of your settings.py file.

More information can be achieved for this link — https://devcenter.heroku.com/articles/django-app-configuration

With this, your Django app is fully configured for Heroku. Let’s get deploying!

Deploying your app

Now that the setup is done, let’s deploy. First, check if git is working for you by typing git — git --version . Also, check if Heroku CLI is working by just typing heroku in the cmd line. If you get an error message, you will probably have to add git and Heroku CLI to your path variable in advanced settings.

If the above commands are working, then let’s get started. First, you will have to initialize git. This is done by typing the following command —

git init

Once you have initialized, you must log in to Heroku via the cmd line. Log in by typing “Heroku login” and it will open a web page for you, where you will click a button and log in.

After login, you must type the following command —

heroku create mydomainname

Here, “mydomainname” is the name domain name that will be visible to everyone on the internet. It is basically the URL name to your website. You can give your project’s name here too. If the name is already taken, then it will display the error on your cmd line and ask you to change the name.

Once your domain name is created, it will display on the cmd line as output and you can actually paste it on a browser and see if the link is working. It will display a Heroku page. The web URL will look something like — mydomainname.herokuapp.com

Now we must link our git to Heroku CLI. This can be done by the following command —

heroku git:remote -a mydomainname

Once this is done, the only step left is to push your code to Heroku.

We use git commands to commit our code and then push it to Heroku. If you are not familiar with git commands, you can check their website and tutorials.

To add all the projects to the commit, we run “git add . “ or “git add -all”. To view the files added, we can run “git status”, which will display all the new files added in green.

Next to commit the code, we run “git commit -m “Inital Commit”. -m represents the message you want to send with the commit.

And finally, we push our code to Heroku, by running “git push heroku master”

This command will take some time to execute and run, but Congratulations! Your code is deployed. Once the cmd finishes, it displays a message saying “Deployed” and you can actually open up the URL “mydomainname.herokuapp.com” and see your website running there.

There are a few more steps to be followed after this. The database on the server will not be updated yet and will be giving you an error even after deployment. This is because we need to migrate the database definition from models.py. This is done by the following command —

heroku run python manage.py migrate

Now your database should also be set up and the website should be rendered properly! You can go to your dashboard on Heroku website and stats of your website.

You can see my website on http://rajatkeshri.herokuapp.com.

Thanks a lot for reading. For any queries, you can ask in the comment section below. Cheers!

Deploy Your Django Project for FREE! (2024)
Top Articles
Latest Posts
Article information

Author: Geoffrey Lueilwitz

Last Updated:

Views: 6682

Rating: 5 / 5 (60 voted)

Reviews: 83% of readers found this page helpful

Author information

Name: Geoffrey Lueilwitz

Birthday: 1997-03-23

Address: 74183 Thomas Course, Port Micheal, OK 55446-1529

Phone: +13408645881558

Job: Global Representative

Hobby: Sailing, Vehicle restoration, Rowing, Ghost hunting, Scrapbooking, Rugby, Board sports

Introduction: My name is Geoffrey Lueilwitz, I am a zealous, encouraging, sparkling, enchanting, graceful, faithful, nice person who loves writing and wants to share my knowledge and understanding with you.