How to Change Django Version in Python

Django is a popular web development framework that allows you to quickly build and develop Python based websites and applications. It is frequently updated with latest features, patches and bug fixes. So it if your website is built using Django, then it is important to update its version from time to time. In this article, we will learn how to change Django version in Python.

Why Change Django Version

There are several reasons why it is essential to change Django version. Some of the key ones are:

  1. Avail new features not present in your current version
  2. Avail feature improvements to existing features
  3. Benefit from performance improvements
  4. Include bug fixes and patches
  5. Regular upgrade is better than doing it once in a while, since it may break your code.
  6. Increase security and protection from cyber attacks.
  7. Data protection and better encryption features
  8. Newer versions offer productivity tools and tricks not available in older versions
  9. Several add-ons, libraries and plugins may not be supported for older versions
  10. Rollback to older version in case an upgrade broke your code

Check Django Version

Before we change Django to a different version, it is important to find out the current Django version running on our server. There are several ways to do this.

First, open terminal and run Python program in it.

$ python

Then run the following command to import Django library and check its version. It will display the Django version on the next line.

>>> import django
>>> django.get_version()
3.9.2

Alternatively, you can navigate to your Django project directory and run the following command to open Python shell.

$ python manage.py shell

Then you can import Django library as shown above and run get_version() function.

>>> import django
>>> django.get_version()
3.9.2

You can also run the following command directly from your Django project’s directory.

$ python manage.py version
3.9.2

You can also run ‘pip freeze’ command to get a list of all Python modules installed on your system, along with their version numbers. We will pass its output through grep to get only information related to Django.

$ pip freeze | grep 'Django'
Django==3.9.2

How to Change Django Version in Python

Most of the times, you will need to update your Django to latest version. Sometimes, if your current Django version gives you problems then you may need to downgrade it to an older version. We will look at both these approaches. Now let us look at some of the simple ways to easily change Django version.

Update Django Version

Django has many dependencies. When you upgrade your Django version, you may also need to update these dependencies, in case they do not support the new version. In most cases, when you update Django, its dependencies are also updated. But you need to be careful, that these dependencies do not break other software that also depend on them.

Nevertheless, here are the 3 commands to upgrade Django to the latest version.

$ pip install django --upgrade
#OR
$ python -m pip install -U django
#OR
$ python -m pip install --upgrade django

In each of the above commands, Django will be automatically updated to the latest version, and its dependencies will also be updated to support this version.

If you want to upgrade Django not to the latest version, but a specific version, then you can specify it. Here is an example to update Django to version 4.0.2.

$ pip install --upgrade django==4.0.2
#OR
$ python -m pip install -U django==4.0.2
#OR
$ python -m pip install --upgrade django==4.0.2

After the upgrade, run the following command to check its version again.

$ python manage.py version

Downgrade Django Version

Sometimes, your current Django version may break your code, or your Django update may not have gone as expected. In such cases, you will need to downgrade Django to a lower version that was working properly. You can do this using the above mentioned commands, where we specify the version number. Let us say your current Django version is 3.9.2 and you want to downgrade it to 3.7.2, then here is the command to do this.

$ pip install --upgrade django==3.7.2
#OR
$ python -m pip install -U django==3.7.2
#OR
$ python -m pip install --upgrade django==3.7.2

Here also, once you have downgraded your Django, check its version as described earlier.

$ python manage.py version
3.7.2

In this case also, you need to check if any dependency has been modified and whether

Conclusion

In this article, we have learnt how to change Django version in Python. We learnt how to upgrade it to latest version, as well as to a specific version. We also learnt how to downgrade Django to a specific version. It is important to regularly update Django server version on your system, to get benefit of latest features, patches, and bug fixes, and also performance improvements.

Also read:
How to Write Pandas Dataframe to Excel Spreadsheets
How to Find Duplicates in Pandas Dataframe
How to Merge & Join Pandas Dataframe

Leave a Reply

Your email address will not be published. Required fields are marked *