{"id":329321,"date":"2023-10-04T12:41:19","date_gmt":"2023-10-04T12:41:19","guid":{"rendered":"https:\/\/pythonguides.com\/?p=16504"},"modified":"2023-10-04T12:41:19","modified_gmt":"2023-10-04T12:41:19","slug":"python-django-set-timezone","status":"publish","type":"post","link":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/2023\/10\/04\/python-django-set-timezone\/","title":{"rendered":"Python Django Set Timezone"},"content":{"rendered":"<p class=\"syndicated-attribution\"><meta name= \\\"keywords \\\" content= \\\"\u96fb\u5b50\u8a08\u7b97\u6a5f, \u6559\u80b2, IT \u96fb\u8166\u73ed,\u96fb\u8166\u88dc\u7fd2\uff0c \u96fb\u8166\u73ed\uff0c \u5bb6\u6559\uff0c \u79c1\u4eba\u8001\u5e2b\uff0c \u8cc7\u8a0a\u6280\u8853\uff0c \u7a0b\u5e8f\u8a2d\u8a08\uff0c \u96fb\u5b50\u8a08\u7b97\u6a5f\uff0c \u904a\u6232\uff0c \u860b\u679c\uff0c \u96fb\u5f71\uff0c \u8a08\u7b97\u6a5f\uff0c\u7de8\u78bc\uff0c Java\uff0c C\/C++\uff0c JavaScript\uff0c PHP\uff0c HTML\uff0c CSS\uff0c MySQL\uff0c mobile\uff0c Android\uff0c \u52d5\u6f2b\uff0c Python\uff0c teacher\uff0c \u88dc\u7fd2\uff0c \u96fb\u8166\u88dc\u7fd2 \u8cc7\u8a0a, \u7535\u5b50\u8ba1\u7b97\u673a, IT ,Game, apple, movie, Computer,student,Java,\u6559\u80b2, ,\u5b66\u751f, \u5b66\u4e60, learn, \u6559\u5b66,  Android, apple,anime, animation, \u4fe1\u606f\u6280\u672f, \u7a0b\u5e8f\u8bbe\u8ba1, \u79fb\u52a8\u7535\u8bdd, \u8cc7\u8a0a\u79d1\u6280,Game, Jeu, Juego,Call Of Duty ,\u4f7f\u547d\u53ec\u559a , \u6e38\u620f, \u7535\u5b50\u6e38\u620f,, \u591a\u4eba\u7535\u5b50\u6e38\u620f, \u7f51\u7edc\u6e38\u620f\uff0conline\uff0conline game, \u624b\u673a\u6e38\u620f, mobile \\\"><\/p>\n<p>In this <a class=\"rank-math-link\" href=\"https:\/\/pythonguides.com\/what-is-python-django\/\"  rel=\"noreferrer noopener\">Django Tutorial<\/a>, you will learn about <strong>Python Django Set Timezone<\/strong> where you understand the concept of timezone.<\/p>\n<p>Also you will understand the importance of timezone in the Django applications. Additonally you learn how to set timezone in your Django project. <\/p>\n<p>Finally you understand how to set timezone for a specific region.<\/p>\n<div class=\"adthrive-video-player in-post\" itemscope itemtype=\"https:\/\/schema.org\/VideoObject\" data-video-id=\"JDIxMCLm\" data-player-type=\"default\" override-embed=\"default\">\n\t\t\t<meta itemprop=\"uploadDate\" content=\"2022-06-20T07:05:42.000Z\" \/><br \/>\n\t\t<meta itemprop=\"name\" content=\"How to Set Timezone in Python Django\" \/><br \/>\n\t\t<meta itemprop=\"description\" content=\"This video tutorial explained, how to set timezone in Python Django.\" \/><br \/>\n\t\t<meta itemprop=\"thumbnailUrl\" content=\"https:\/\/content.jwplatform.com\/thumbs\/JDIxMCLm-720.jpg\" \/><br \/>\n\t\t<meta itemprop=\"contentUrl\" content=\"https:\/\/content.jwplatform.com\/videos\/JDIxMCLm.mp4\" \/>\n\t<\/div>\n<h2 class=\"wp-block-heading\" id=\"python-django-set-timezone\">Python Django Set Timezone<\/h2>\n<p>A time zone is just a region of the globe where everyone is on the same clock. However, due to something called daylight savings time, a given time zone may not follow the same time throughout the year.<\/p>\n<p>Because of these issues, the globe agreed to adopt a universal time standard that remains constant regardless of where you are or what time of year it is. The <strong>coordinated universal time, or UTC<\/strong>, is the name for this.<\/p>\n<p><strong>For example, I live in India<\/strong>, which is 5 hours and 30 minutes ahead of UTC, so the time was <strong>10:32:10 PM UTC+5:30<\/strong> at this&nbsp;time. <strong>10:32:10 PM GMT+5:30<\/strong> is another way to write it. There is no difference between GMT and UTC in terms of time; nonetheless, GMT is a time zone, and UTC is a time measurement standard.<\/p>\n<h3 class=\"wp-block-heading\">Why Timezone is Importand?<\/h3>\n<p>When you properly handle the time zones it make sure that the user see date and time data according to their location or whereever they live.<\/p>\n<p>The time zons are very important in the appliacatin or project where timely operation are performed such as airline ticket booking system, an e-commerce application such as amazone for time limited offers.<\/p>\n<p>Also, check: <a href=\"https:\/\/pythonguides.com\/how-to-get-current-time-in-django\/\"  aria-label=\" (opens in a new tab)\" rel=\"noreferrer noopener\" class=\"rank-math-link\">How to Get Current time in Django<\/a><\/p>\n<h3 class=\"wp-block-heading\" id=\"python-django-set-timezone-settings\">Python Django Set Timezone Settings<\/h3>\n<p>For saving information in the database, Django recommends using UTC. Even if your website is only accessible in a one-time zone, storing data in UTC in your database is still a good strategy. Daylight Saving Time is the primary reason for this. <\/p>\n<p><strong>DST<\/strong> is a time-keeping system in which clocks are pushed forward in the spring and backward in the autumn in many nations. If you work in local time, you&#8217;re likely to run into problems twice a year when the clocks change.<\/p>\n<p>When you build Django project, it automatically created a a file called setting.py in your project directory.<\/p>\n<p>Let&#8217;s build Django project and see how to change the timezones.<\/p>\n<p>First open your command prompt and create a virtual environment named &#8216;env&#8217;.<\/p>\n<pre class=\"wp-block-code\"><code>python -m venv env<\/code><\/pre>\n<p>Activate the environment.<\/p>\n<pre class=\"wp-block-code\"><code>env\\Scripts\\activate<\/code><\/pre>\n<p>Install the latest version of Django.<\/p>\n<pre class=\"wp-block-code\"><code>pip install django<\/code><\/pre>\n<p>Create a Django project named &#8216;django_timezones&#8217;.<\/p>\n<pre class=\"wp-block-code\"><code>django-admin startproject django_timezones<\/code><\/pre>\n<p>Change the directory to the Django project.<\/p>\n<pre class=\"wp-block-code\"><code>cd django_timezones<\/code><\/pre>\n<p>Create a Django app named <strong>&#8216;timezone_app&#8217;<\/strong>.<\/p>\n<pre class=\"wp-block-code\"><code>python manage.py startapp timezone_app<\/code><\/pre>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"628\" src=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/10\/Python-Django-Set-Timezone-Settings-Project-Setup-1024x628.jpg\" alt=\"Python Django Set Timezone Settings Project Setup\" class=\"wp-image-45656\" srcset=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/10\/Python-Django-Set-Timezone-Settings-Project-Setup-1024x628.jpg 1024w, https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/10\/Python-Django-Set-Timezone-Settings-Project-Setup-300x184.jpg 300w, https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/10\/Python-Django-Set-Timezone-Settings-Project-Setup-768x471.jpg 768w, https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/10\/Python-Django-Set-Timezone-Settings-Project-Setup.jpg 1498w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" title=\"Python Django Set Timezone Settings Project Setup\"><\/figure>\n<\/div>\n<p>Add the created app in the <strong>INSTALLED_APPS<\/strong> section of setting.py file of your Django project  <strong>&#8216;django_timzeons&#8217;<\/strong> as shown below.<\/p>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"473\" src=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/10\/Python-Django-Set-Timezone-Settings-Installed-Apps-1024x473.jpg\" alt=\"Python Django Set Timezone Settings Installed Apps\" class=\"wp-image-45662\" srcset=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/10\/Python-Django-Set-Timezone-Settings-Installed-Apps-1024x473.jpg 1024w, https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/10\/Python-Django-Set-Timezone-Settings-Installed-Apps-300x139.jpg 300w, https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/10\/Python-Django-Set-Timezone-Settings-Installed-Apps-768x355.jpg 768w, https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/10\/Python-Django-Set-Timezone-Settings-Installed-Apps.jpg 1526w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" title=\"Python Django Set Timezone Settings Installed Apps\"><\/figure>\n<\/div>\n<p>Open the <strong>urls.py<\/strong> file of your Django project and add the following lines of code.<\/p>\n<pre class=\"wp-block-code\"><code>from django.contrib import admin\nfrom django.urls import path,include\n\nurlpatterns = &#091;\n    path('admin\/', admin.site.urls),\n    path('',include('timezone_app.urls'))\n]<\/code><\/pre>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"443\" src=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/10\/Python-Django-Set-Timezone-Settings-URL-1024x443.jpg\" alt=\"Python Django Set Timezone Settings URL\" class=\"wp-image-45663\" srcset=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/10\/Python-Django-Set-Timezone-Settings-URL-1024x443.jpg 1024w, https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/10\/Python-Django-Set-Timezone-Settings-URL-300x130.jpg 300w, https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/10\/Python-Django-Set-Timezone-Settings-URL-768x333.jpg 768w, https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/10\/Python-Django-Set-Timezone-Settings-URL.jpg 1425w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" title=\"Python Django Set Timezone Settings URL\"><\/figure>\n<\/div>\n<p>Now open your project in visual studio code.<\/p>\n<p>After that open the Django project named <strong>&#8216;django_timezones&#8217;<\/strong> and there is file called <strong>setting.py<\/strong>.<\/p>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"653\" height=\"577\" src=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/10\/Python-Django-Set-Timezone-Settings-File.jpg\" alt=\"Python Django Set Timezone Settings File\" class=\"wp-image-45657\" srcset=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/10\/Python-Django-Set-Timezone-Settings-File.jpg 653w, https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/10\/Python-Django-Set-Timezone-Settings-File-300x265.jpg 300w\" sizes=\"(max-width: 653px) 100vw, 653px\" title=\"Python Django Set Timezone Settings File\"><\/figure>\n<\/div>\n<p>You only need to configure a few things in your Django application&#8217;s <strong>settings.py<\/strong> to set the timezone.<\/p>\n<pre class=\"wp-block-code\"><code>TIME_ZONE = '&lt;Time zone of your choice&gt;'\nUSE_TZ = True<\/code><\/pre>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"610\" src=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/10\/Python-Django-Set-Timezone-Settings-1024x610.jpg\" alt=\"Python Django Set Timezone Settings\" class=\"wp-image-45659\" srcset=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/10\/Python-Django-Set-Timezone-Settings-1024x610.jpg 1024w, https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/10\/Python-Django-Set-Timezone-Settings-300x179.jpg 300w, https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/10\/Python-Django-Set-Timezone-Settings-768x458.jpg 768w, https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/10\/Python-Django-Set-Timezone-Settings.jpg 1421w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" title=\"Python Django Set Timezone Settings\"><\/figure>\n<\/div>\n<p>By default, the Time zone is <strong>UTC<\/strong>, and <strong>USE_TZ<\/strong> is set to <strong>True<\/strong>, which ensures that using a <strong>datetime.now()<\/strong> function in your Django application creates time in UTC.<\/p>\n<p>The UTC stands for Coordinated Universal Time and on the basis of this time standard world controls the clocks and times.<\/p>\n<p>Read: <a href=\"https:\/\/pythonguides.com\/create-model-in-django\/\"  aria-label=\" (opens in a new tab)\" rel=\"noreferrer noopener\" class=\"rank-math-link\">How to Create model in Django<\/a><\/p>\n<h3 class=\"wp-block-heading\" id=\"python-django-set-timezone-utc\">Python Django Set Timezone UTC<\/h3>\n<p>Now, in this section, we will discuss how to set the timezone to UTC. For the demonstration of this topic, we are using the <strong>Delivery<\/strong> model in Django. And the model contains the following data.<\/p>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"218\" src=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2022\/02\/django-set-timezone-utc-1024x218.png\" alt=\"Python Django set timezone\" class=\"wp-image-25936\" srcset=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2022\/02\/django-set-timezone-utc-1024x218.png 1024w, https:\/\/pythonguides.com\/wp-content\/uploads\/2022\/02\/django-set-timezone-utc-300x64.png 300w, https:\/\/pythonguides.com\/wp-content\/uploads\/2022\/02\/django-set-timezone-utc-768x163.png 768w, https:\/\/pythonguides.com\/wp-content\/uploads\/2022\/02\/django-set-timezone-utc.png 1241w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" title=\"django set timezone utc\"><figcaption class=\"wp-element-caption\">Delivery Model<\/figcaption><\/figure>\n<\/div>\n<p>Django&#8217;s timezone is set to UTC by default. If your timezone is not UTC, you can change it using the procedure&nbsp;below:<\/p>\n<ul>\n<li>Open the setting.py file in the directory of our project.<\/li>\n<li>Internationalization can be found by scrolling down the file.<\/li>\n<li>TIME ZONE will appear under this.<\/li>\n<li>Set it to &#8216;UTC&#8217;.<\/li>\n<\/ul>\n<p><strong>Command to set timezone:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>TIME_ZONE='UTC'<\/code><\/pre>\n<p>Now you may start the server and log in to the admin interface. After that, open your Delivery model, you&#8217;ll notice that the time has been changed to UTC.<\/p>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"214\" src=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2022\/02\/django-set-timezone-to-utc-1024x214.png\" alt=\"django set timezone to utc\" class=\"wp-image-25937\" srcset=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2022\/02\/django-set-timezone-to-utc-1024x214.png 1024w, https:\/\/pythonguides.com\/wp-content\/uploads\/2022\/02\/django-set-timezone-to-utc-300x63.png 300w, https:\/\/pythonguides.com\/wp-content\/uploads\/2022\/02\/django-set-timezone-to-utc-768x160.png 768w, https:\/\/pythonguides.com\/wp-content\/uploads\/2022\/02\/django-set-timezone-to-utc.png 1308w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" title=\"django set timezone to utc\"><figcaption class=\"wp-element-caption\">TIME_ZONE = UTC<\/figcaption><\/figure>\n<\/div>\n<p>Read: <a href=\"https:\/\/pythonguides.com\/python-django-get-admin-password\/\"  aria-label=\" (opens in a new tab)\" rel=\"noreferrer noopener\" class=\"rank-math-link\">Python Django get admin password<\/a><\/p>\n<h3 class=\"wp-block-heading\" id=\"python-django-set-timezone-to-est\">Python Django Set Timezone To EST<\/h3>\n<p>Now, in this section, we will discuss how to set the timezone to EST. To illustrate this topic, we are using the <strong>Delivery model<\/strong> in Django. And the model contains the following data and here the time zone used in this model is UTC, by default.<\/p>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"214\" src=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2022\/02\/django-set-timezone-to-utc-1024x214.png\" alt=\"django set timezone to utc\" class=\"wp-image-25937\" srcset=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2022\/02\/django-set-timezone-to-utc-1024x214.png 1024w, https:\/\/pythonguides.com\/wp-content\/uploads\/2022\/02\/django-set-timezone-to-utc-300x63.png 300w, https:\/\/pythonguides.com\/wp-content\/uploads\/2022\/02\/django-set-timezone-to-utc-768x160.png 768w, https:\/\/pythonguides.com\/wp-content\/uploads\/2022\/02\/django-set-timezone-to-utc.png 1308w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" title=\"django set timezone to utc\"><figcaption class=\"wp-element-caption\">Delivery Model<\/figcaption><\/figure>\n<\/div>\n<p>To set the timezone, you must first set up a few things in your Django application&#8217;s settings.py, which are as follows:<\/p>\n<p><strong>Command to set timezone:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>TIME_ZONE = 'EST'<\/code><\/pre>\n<p>You can now start the server and access the administrative interface. When you open your Delivery model,&nbsp;you&#8217;ll see that the time has been changed to EST.<\/p>\n<p>Here is the output, which clearly shows that the time zone changes to EST.<\/p>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"191\" src=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2022\/02\/django-set-timezone-to-est-1024x191.png\" alt=\"django set timezone to est\" class=\"wp-image-25938\" srcset=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2022\/02\/django-set-timezone-to-est-1024x191.png 1024w, https:\/\/pythonguides.com\/wp-content\/uploads\/2022\/02\/django-set-timezone-to-est-300x56.png 300w, https:\/\/pythonguides.com\/wp-content\/uploads\/2022\/02\/django-set-timezone-to-est-768x143.png 768w, https:\/\/pythonguides.com\/wp-content\/uploads\/2022\/02\/django-set-timezone-to-est.png 1344w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" title=\"django set timezone to est\"><figcaption class=\"wp-element-caption\">TIME_ZONE = EST<\/figcaption><\/figure>\n<\/div>\n<p>Read: <a href=\"https:\/\/pythonguides.com\/if-statement-in-django-template\/\"  aria-label=\" (opens in a new tab)\" rel=\"noreferrer noopener\" class=\"rank-math-link\">If statement in Django template<\/a><\/p>\n<h3 class=\"wp-block-heading\" id=\"python-django-set-timezone-to-ist\">Python Django Set Timezone To IST<\/h3>\n<p>Now, in this section, we will discuss how to set the timezone to IST. To illustrate this topic, we are using the <strong>Delivery model<\/strong> in Django. And the model contains the following data and here the time zone used in this model is UTC, by default.<\/p>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"214\" src=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2022\/02\/django-set-timezone-to-utc-1-1024x214.png\" alt=\"django set timezone to utc\" class=\"wp-image-25945\" srcset=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2022\/02\/django-set-timezone-to-utc-1-1024x214.png 1024w, https:\/\/pythonguides.com\/wp-content\/uploads\/2022\/02\/django-set-timezone-to-utc-1-300x63.png 300w, https:\/\/pythonguides.com\/wp-content\/uploads\/2022\/02\/django-set-timezone-to-utc-1-768x160.png 768w, https:\/\/pythonguides.com\/wp-content\/uploads\/2022\/02\/django-set-timezone-to-utc-1.png 1308w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" title=\"django set timezone to utc 1\"><figcaption class=\"wp-element-caption\">Delivery Model<\/figcaption><\/figure>\n<\/div>\n<p>Here IST means Indian Standard Time, which is Asia\/Kolkata as per the time zone. And, to set the timezone, you must first set up a few things in your Django application&#8217;s settings.py, which are as follows:<\/p>\n<p><strong>Command to set timezone:<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>TIME_ZONE = 'Asia\/Kolkata'<\/code><\/pre>\n<p>You can now start the server and access the administrative interface. When you open your Delivery model,&nbsp;you&#8217;ll see that the time has been changed to IST.<\/p>\n<p>Here is the output, which clearly shows that the time zone changes to IST.<\/p>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"199\" src=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2022\/02\/django-set-timezone-ist-1024x199.png\" alt=\"django set timezone ist\" class=\"wp-image-25946\" srcset=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2022\/02\/django-set-timezone-ist-1024x199.png 1024w, https:\/\/pythonguides.com\/wp-content\/uploads\/2022\/02\/django-set-timezone-ist-300x58.png 300w, https:\/\/pythonguides.com\/wp-content\/uploads\/2022\/02\/django-set-timezone-ist-768x149.png 768w, https:\/\/pythonguides.com\/wp-content\/uploads\/2022\/02\/django-set-timezone-ist.png 1300w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" title=\"django set timezone ist\"><figcaption class=\"wp-element-caption\">TIME_ZONE = IST<\/figcaption><\/figure>\n<\/div>\n<p>Read: <a href=\"https:\/\/pythonguides.com\/get-url-parameters-in-django\/\"  aria-label=\" (opens in a new tab)\" rel=\"noreferrer noopener\" class=\"rank-math-link\">Get URL parameters in Django<\/a><\/p>\n<h3 class=\"wp-block-heading\" id=\"python-django-set-timezone-for-user\">Python Django Set Timezone For User<\/h3>\n<p>In this section, we&#8217;ll learn how to set the timezone for users based on their local timestamp. If you want to create timestamps in the local time zone. Do the following settings in the <strong>settings.py<\/strong> file of the Django project.<\/p>\n<pre class=\"wp-block-code\"><code>USE_TZ = FALSE<\/code><\/pre>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"434\" src=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/10\/Python-Django-Set-Timezone-For-User-USE_TZ-1024x434.jpg\" alt=\"Python Django Set Timezone For User USE_TZ\" class=\"wp-image-45661\" srcset=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/10\/Python-Django-Set-Timezone-For-User-USE_TZ-1024x434.jpg 1024w, https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/10\/Python-Django-Set-Timezone-For-User-USE_TZ-300x127.jpg 300w, https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/10\/Python-Django-Set-Timezone-For-User-USE_TZ-768x325.jpg 768w, https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/10\/Python-Django-Set-Timezone-For-User-USE_TZ.jpg 1185w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" title=\"Python Django Set Timezone For User USE TZ\"><\/figure>\n<\/div>\n<p>If you set <strong>USE_TZ<\/strong> to <strong>False<\/strong>, Django assumes your application doesn&#8217;t care about time zones. It refers to the location where the Django application is hosted; for example, if you run it locally on your computer, it will use that time.<\/p>\n<p>By default, the <strong>TIME_ZONE<\/strong> is set to <strong>&#8216;UTC&#8217;<\/strong>, and the <strong>USE_TZ<\/strong> is set to <strong>&#8216;True&#8217;<\/strong>. So, when we fetch the time it shows the time in UTC.<\/p>\n<p>Let&#8217;s see an example to understand the concept clearly:<\/p>\n<p>Open the the <strong>views.py<\/strong> file of your Django app <strong>&#8216;timezone_app&#8217;<\/strong> and add the following lines of code.<\/p>\n<pre class=\"wp-block-code\"><code>\nfrom django.http import HttpResponse\nfrom django.utils import timezone\n\n\ndef home(request):\n    current_time = timezone.now().strftime('%H:%M:%S')\n    html = \"&lt;html&gt;&lt;body&gt;&lt;b&gt;Current Time Value:&lt;\/b&gt; %s&lt;\/body&gt;&lt;\/html&gt;\" % current_time\n    return HttpResponse(html)<\/code><\/pre>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"339\" src=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/10\/Python-Django-Set-Timezone-For-User-Views-1024x339.jpg\" alt=\"Python Django Set Timezone For User Views\" class=\"wp-image-45664\" srcset=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/10\/Python-Django-Set-Timezone-For-User-Views-1024x339.jpg 1024w, https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/10\/Python-Django-Set-Timezone-For-User-Views-300x99.jpg 300w, https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/10\/Python-Django-Set-Timezone-For-User-Views-768x254.jpg 768w, https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/10\/Python-Django-Set-Timezone-For-User-Views-1536x508.jpg 1536w, https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/10\/Python-Django-Set-Timezone-For-User-Views.jpg 1567w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" title=\"Python Django Set Timezone For User Views\"><\/figure>\n<\/div>\n<p>In the above example, we have imported the&nbsp;<strong>timezone<\/strong>&nbsp;class from the&nbsp;<strong>django.utils<\/strong> module. Next, we have created a view with the name&nbsp;<strong>home<\/strong>. In this view, we are using the&nbsp;<strong>timezone.now().strftime()<\/strong>&nbsp;method to store the current time value in a variable. And then, we are using the variable to return the current time value as an HTTP response.<\/p>\n<p>After that create a urls.py file in your Django app <strong>&#8216;timzone_app&#8217;<\/strong> and add the following URL path.<\/p>\n<pre class=\"wp-block-code\"><code>from django.urls import path\nfrom .views import *\nurlpatterns = &#091;\n    path('', home,name='home'),\n]<\/code><\/pre>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"961\" height=\"475\" src=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/10\/Python-Django-Set-Timezone-For-User-App-URL.jpg\" alt=\"Python Django Set Timezone For User App URL\" class=\"wp-image-45665\" srcset=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/10\/Python-Django-Set-Timezone-For-User-App-URL.jpg 961w, https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/10\/Python-Django-Set-Timezone-For-User-App-URL-300x148.jpg 300w, https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/10\/Python-Django-Set-Timezone-For-User-App-URL-768x380.jpg 768w\" sizes=\"(max-width: 961px) 100vw, 961px\" title=\"Python Django Set Timezone For User App URL\"><\/figure>\n<\/div>\n<p>Opne your terminal and run the following command to migrate the defaul model that comes with Django.<\/p>\n<pre class=\"wp-block-code\"><code>python manage.py migrate<\/code><\/pre>\n<p>Run the Django server using the below command.<\/p>\n<pre class=\"wp-block-code\"><code>python manage.py runserver<\/code><\/pre>\n<p>Now, if we run the development server and open the URL <strong>&#8216;http:\/\/127.0.0.1:8000\/&#8217;<\/strong> in your browser, you see the following output.<\/p>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"611\" height=\"260\" src=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/10\/Python-Django-Set-Timezone-For-User.jpg\" alt=\"Python Django Set Timezone For User\" class=\"wp-image-45667\" srcset=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/10\/Python-Django-Set-Timezone-For-User.jpg 611w, https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/10\/Python-Django-Set-Timezone-For-User-300x128.jpg 300w\" sizes=\"(max-width: 611px) 100vw, 611px\" title=\"Python Django Set Timezone For User\"><\/figure>\n<\/div>\n<p>If you user belong to speicific region such user lives in New York, to set the timezone for that user.<\/p>\n<p>Open the <strong>setting.py<\/strong> file of your Django project <strong>&#8216;django_timezones&#8217;<\/strong> and set the <strong>TIME_ZONE<\/strong> to <strong>&#8216;American\/New_York&#8217;<\/strong> and <strong>USE_TZ<\/strong> to <strong>True<\/strong> as shown in the below picture.<\/p>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"340\" src=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/10\/Python-Django-Set-Timezone-For-User-New-York-1024x340.jpg\" alt=\"Python Django Set Timezone For User New York\" class=\"wp-image-45670\" srcset=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/10\/Python-Django-Set-Timezone-For-User-New-York-1024x340.jpg 1024w, https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/10\/Python-Django-Set-Timezone-For-User-New-York-300x100.jpg 300w, https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/10\/Python-Django-Set-Timezone-For-User-New-York-768x255.jpg 768w, https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/10\/Python-Django-Set-Timezone-For-User-New-York.jpg 1198w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" title=\"Python Django Set Timezone For User New York\"><\/figure>\n<\/div>\n<p>Open the <strong>views.py<\/strong> file of your Django app <strong>&#8216;timezon_app&#8217;<\/strong> and modify the view <strong>home<\/strong> by adding the following lines of code:<\/p>\n<pre class=\"wp-block-code\"><code>from django.http import HttpResponse\nfrom django.utils import timezone\n\n\n\ndef home(request):\n    current_time = timezone.now().strftime('%H:%M:%S')\n    html = \"&lt;html&gt;&lt;body&gt;&lt;b&gt;Current Time Value:&lt;\/b&gt; %s&lt;\/body&gt;&lt;\/html&gt;\" % current_time\n    return HttpResponse(html)<\/code><\/pre>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"292\" src=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/10\/Python-Django-Set-Timezone-For-User-View-Home-1024x292.jpg\" alt=\"Python Django Set Timezone For User View Home\" class=\"wp-image-45668\" srcset=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/10\/Python-Django-Set-Timezone-For-User-View-Home-1024x292.jpg 1024w, https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/10\/Python-Django-Set-Timezone-For-User-View-Home-300x86.jpg 300w, https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/10\/Python-Django-Set-Timezone-For-User-View-Home-768x219.jpg 768w, https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/10\/Python-Django-Set-Timezone-For-User-View-Home.jpg 1525w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" title=\"Python Django Set Timezone For User View Home\"><\/figure>\n<\/div>\n<p>Now again open the URL <strong>&#8216;http:\/\/127.0.0.1:8000\/&#8217;<\/strong> in your browser, you get the following output.<\/p>\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"611\" height=\"222\" src=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/10\/Python-Django-Set-Timezone-For-User-American-New-York.jpg\" alt=\"Python Django Set Timezone For User American New York\" class=\"wp-image-45671\" srcset=\"https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/10\/Python-Django-Set-Timezone-For-User-American-New-York.jpg 611w, https:\/\/pythonguides.com\/wp-content\/uploads\/2023\/10\/Python-Django-Set-Timezone-For-User-American-New-York-300x109.jpg 300w\" sizes=\"(max-width: 611px) 100vw, 611px\" title=\"Python Django Set Timezone For User American New York\"><\/figure>\n<\/div>\n<p>The above time shows according the timezone <strong>&#8216;American\/New_York&#8217;<\/strong>.<\/p>\n<figure class=\"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\">\n<div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"How to Set Timezone in Python Django\" width=\"975\" height=\"548\" src=\"https:\/\/www.youtube.com\/embed\/5u9V5XulvqI?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" allowfullscreen><\/iframe>\n<\/div>\n<\/figure>\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n<p>In this Django tutorial, you learned how to set timezone in Django project and also learned the importance of timezone. Finally you build the Djang project and set the timezone to different time.<\/p>\n<p>You may also like to read the following Django tutorials.<\/p>\n<ul>\n<li><a aria-label=\" (opens in a new tab)\" class=\"rank-math-link\" href=\"https:\/\/pythonguides.com\/django-random-number\/\"  rel=\"noreferrer noopener\">Python Django random number<\/a><\/li>\n<li><a aria-label=\" (opens in a new tab)\" class=\"rank-math-link\" href=\"https:\/\/pythonguides.com\/get-data-from-get-request-in-django\/\"  rel=\"noreferrer noopener\">How to get data from get request in Django<\/a><\/li>\n<li><a aria-label=\" (opens in a new tab)\" class=\"rank-math-link\" href=\"https:\/\/pythonguides.com\/python-django-get\/\"  rel=\"noreferrer noopener\">Python Django get \u2013 Everything you need to know<\/a><\/li>\n<li><a aria-label=\" (opens in a new tab)\" class=\"rank-math-link\" href=\"https:\/\/pythonguides.com\/outputting-python-to-html-django\/\"  rel=\"noreferrer noopener\">Outputting Python to html Django<\/a><\/li>\n<li><a aria-label=\" (opens in a new tab)\" class=\"rank-math-link\" href=\"https:\/\/pythonguides.com\/compare-two-integers-in-python-django\/\"  rel=\"noreferrer noopener\">Compare two integers in Python Django<\/a><\/li>\n<\/ul>\n\n<p class=\"syndicated-attribution\"><figure class= \\\"wp-block-image alignnone \\\"><img src= \\\"http:\/\/itteacheritfreelance.hk\/test\/wordpress\/wp-content\/uploads\/2016\/05\/logo2-2.png\\\" alt=\\\"IT\u96fb\u8166\u88dc\u7fd2 java\u88dc\u7fd2 \u70ba\u5927\u5bb6\u914d\u5c0d\u96fb\u8166\u88dc\u7fd2,IT freelance, \u79c1\u4eba\u8001\u5e2b, PHP\u88dc\u7fd2,CSS\u88dc\u7fd2,XML,Java\u88dc\u7fd2,MySQL\u88dc\u7fd2,graphic design\u88dc\u7fd2,\u4e2d\u5c0f\u5b78ICT\u88dc\u7fd2,\u4e00\u5c0d\u4e00\u79c1\u4eba\u88dc\u7fd2\u548cFreelance\u81ea\u7531\u5de5\u4f5c\u914d\u5c0d\u3002\\\"\/><figcaption>\u7acb\u523b\u8a3b\u518a\u53ca\u5831\u540d\u96fb\u8166\u88dc\u7fd2\u8ab2\u7a0b\u5427!<\/figcaption><\/figure>\r\n<\/br>Find A Teacher Form:\r\n<\/br>https:\/\/docs.google.com\/forms\/d\/1vREBnX5n262umf4wU5U2pyTwvk9O-JrAgblA-wH9GFQ\/viewform?edit_requested=true#responses\r\n<\/br><\/br>Email:\r\n<\/br>public1989two@gmail.com<br><br><br><br><br><br><br>\r\n<a href=www.itsec.hk style=color:#FFFFFF;>www.itsec.hk<\/a><br>\r\n<a href=\\\"www.itsec.vip\\\" style=color:#FFFFFF;>www.itsec.vip<\/a><br>\r\n<a href=\\\"www.itseceu.uk\\\" style=color:#FFFFFF;>www.itseceu.uk<\/a><br><\/p>","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>In this Django Tutorial, you will learn about Python Django Set Timezone where you understand the concept of timezone. Also you will understand the importance of timezone in the Django applications. Additonally you learn how to set timezone in your Django project. Finally you understand how to set timezone for a specific region. Python Django &#8230; <a title=\"Python Django Set Timezone\" class=\"read-more\" href=\"https:\/\/pythonguides.com\/python-django-set-timezone\/\" aria-label=\"More on Python Django Set Timezone\">Read more&#8230;<\/a><\/p>\n<\/div>","protected":false},"author":2034,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"slim_seo":{"title":"Python Django Set Timezone - ITTeacherITFreelance.hk","description":"In this Django Tutorial, you will learn about Python Django Set Timezone where you understand the concept of timezone. Also you will understand the importance o"},"footnotes":""},"categories":[10700],"tags":[10766,10768,10767],"_links":{"self":[{"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/329321"}],"collection":[{"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/users\/2034"}],"replies":[{"embeddable":true,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/comments?post=329321"}],"version-history":[{"count":1,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/329321\/revisions"}],"predecessor-version":[{"id":329322,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/329321\/revisions\/329322"}],"wp:attachment":[{"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/media?parent=329321"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/categories?post=329321"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/tags?post=329321"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}