Upgrade Django to a Newer Version

I’ve encountered many situations that deal with managing the Django version. Whether you’re upgrading to use new features or downgrading to maintain compatibility, knowing how to change the Django version in your Python environment is essential.

In this article, I’ll walk you through simple, practical methods to check your current Django version and switch to the one you need. I’ll also share tips from my own experience to help you avoid common issues.

Let’s get started!

Change Your Django Version

When I first started working with Django, I often faced compatibility issues between projects. Some projects required Django 3.x, while others needed the latest 4.x release. Changing the Django version helps you:

  • Use new features or bug fixes available in newer versions.
  • Maintain compatibility with legacy codebases.
  • Ensure smooth deployment in different environments.

Check out Add a Dropdown Navbar in Django

How to Check Your Current Django Version

Before changing the version, it’s important to know which version you currently have installed. Here’s a quick way to check it:

Open your terminal or command prompt and run:

python -m django --version

This command will print the installed Django version, for example:

5.2.4

You can see the output in the screenshot below.

check django version

Alternatively, you can check the version inside a Python shell:

import django
print(django.get_version())

This will output the Django version as a string.

Method 1: Use pip to Change Django Version

The easy way to change your Django version is by using pip, Python’s package manager.

Step 1: Uninstall the Current Django Version

First, uninstall the existing Django version:

pip uninstall django

You might be prompted to confirm the uninstallation; type y and press Enter.

Step 2: Install the Desired Django Version

Next, install the version you want. For example, to install Django 3.2.18 (a popular LTS version in the USA market), run:

pip install django==3.2.18

You can see the output in the screenshot below.

how to check django version

If you want the latest version, simply run:

pip install django --upgrade

Step 3: Verify the Installation

After installation, verify the version again:

python -m django --version

This should display the Django version you just installed.

Read Create a Function-Based View in Django

Method 2: Use a Virtual Environment to Manage Django Versions

In my projects, I always recommend using virtual environments. They allow you to isolate dependencies and manage different Django versions per project without conflicts.

Step 1: Create a Virtual Environment

Navigate to your project directory and create a virtual environment:

python -m venv venv

Activate it:

  • On Windows:
  venv\Scripts\activate
  • On macOS/Linux:
  source venv/bin/activate

Step 2: Install the Desired Django Version Inside the Virtual Environment

With the virtual environment activated, install the Django version you want:

pip install django==4.1.7

You can see the output in the screenshot below.

pip install specific version

Step 3: Confirm the Version

Run:

python -m django --version

You will see the Django version installed inside this virtual environment.

Why Use Virtual Environments?

  • Keeps project dependencies isolated.
  • Allows multiple projects to use different Django versions.
  • Prevents system-wide package conflicts.

Check out Create a Web Form in Python Django

Method 3: Use pipenv for Version Management

If you prefer a tool that handles both virtual environments and package management, pipenv is a great choice.

Step 1: Install pipenv (if not installed)

pip install pipenv

Step 2: Create a Project with a Specific Django Version

Navigate to your project folder and run:

pipenv install django==3.1.14

This creates a Pipfile locking the Django version.

Step 3: Activate the Pipenv Shell

pipenv shell

Now, inside this shell, you can run:

python -m django --version

Method 4: Change Django Version in Requirements File

For projects with a requirements.txt file, specify the Django version explicitly:

django==4.0.5

Then run:

pip install -r requirements.txt

This ensures all developers or deployment servers use the same Django version.

Read Build a Contact Form in Django using Bootstrap

Tips From My Experience

  • Always test your project after changing the Django version. Some versions introduce breaking changes.
  • Use Long-Term Support (LTS) versions like Django 3.2.x for production projects in the USA; they receive security updates for longer.
  • Keep your requirements.txt or Pipfile updated to avoid version drift.
  • When upgrading major Django versions, review the official Django release notes for backward-incompatible changes.

Changing your Django version in Python is straightforward once you know the right commands and tools. Whether you’re managing a legacy project or starting fresh, controlling your Django version ensures stability and access to the latest features.

You may like to read other Django articles:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.