How to change Timezone in Django

In this tutorial, we will learn how to change or set timezones in Django.

In the new version of Django, it’s very easy to change the timezone.

In settings.py of your project, there is one variable named TIME_ZONE to define the timezone.

Steps to follow:-

  1. Create a project
  2. Open settings.py
  3. Search for TIME_ZONE
  4. Change the timezone.

Create a Project and open settings.py

First, we have to create a Django project. But before that, we need to get the names of timezone so to get this open the link – https://en.wikipedia.org/wiki/List_of_tz_database_time_zones

Or you can simply use the pytz module in python to get the valid names of timezones.-

>>> import pytz 
>>> pytz.all_timezones_set
LazySet({'Africa/Abidjan',
         'America/Eirunepe',
         'Australia/Queensland',
         'Pacific/Port_Moresby',
         'Europe/Saratov',
         'Asia/Calcutta',
         ...
         'UTC',
         'GMT+0',
         'Etc/GMT-3',
         'WET',
         'GMT-0'})

TIME_ZONE in Django

The default timezone is TIME_ZONE = 'UTC'.

You can change this in whichever timezone you want. For example,-

TIME_ZONE = 'Asia/Calcutta'.

For applying these changes we need to set the USE_TZ variable to TRUE.

I hope you find this tutorial helpful.

Leave a Reply

Your email address will not be published.