Introduction

Django is a high-level Python web Framework use it to build scalable web applications, while Heroku is a cloud application platform that facilitates the deployment of web applications.

Heroku supports several programming languages, including Python.

Install the Heroku Toolkit

To install the Heroku Toolkit, create an account on Heroku. Then, install the Heroku Toolbelt. It’s a very powerful CLI that allows users to manage their applications.

After installing the Toolbelt, open your Terminal and login to your account:

Preparing our Application

CD into your application folder.

Here is the list of things you will need to add to your project:

  • Add a Procfile in the project root.
  • Add a requirements.txt file with all the requirements in the project root.
  • Add Gunicorn to the requirements.txt file.
  • Add a runtime.txt to specify the correct Python version in the project root.
  • Configure whitenoise to serve static files.

You may also like: Best Web Hosting Services to Use for Testing Your Code.

The Procfile

Create a file named Procfile in the project root. Add the following content:

Note: change my-application with the name of your Django project.

The Requirements.txt File

Run this command, this command will list all dependencies :

You should see something like this:

The Runtime.txt File

Create a file named runtime.txt in the project root, and put the specific Python version your project uses. For example, this example project will use Python 3.8

Set Up the Static Assets

Configure the STATIC-related parameters in settings.py:

Configure Whitenoise

Install Whitenoise

Add **Whitenoise** to your Django application in the **wsgi.py** file:

Update the **settings.py** file.

Add **whitenoise middleware** at the top of the middleware list in **settings.py**.

Deploy the Application

Update Database Configuration in settings.py (at the bottom of the file).

Create the App in Heroku From Terminal

Choose any name for your app. Heroku will inform you if the name already exists.

Add Your App Domain Name to ALLOWED_HOSTS in Settings.py

Initialize Git and connect your new app (or existing one) to Heroku Git remote repository and push the application.

In your terminal:

If you get an error message with collectstatic, simply disable it by instructing Heroku to ignore running the manage.py collecstatic command during the deployment process.

Then, run:

Migrate the database :

Let me know in the comment section if you have an error. Thank you for reading.

Further Reading



Source link

Write A Comment