6

I am having the following issue, and cant figure out what i am missing here.

I have enabled the Timezone in the settings.py

Settings.py

TIME_ZONE = 'America/Chicago'

USE_TZ = True

Use-case

Now, when I have created an object at 3.15pm (Local time), and i get the following date stored in my created_at field in database.

created_at = models.DateTimeField(auto_now_add=True)

2014-12-12 11:03:48

When i render that date in my templates i get the following value.

{{ image.created_at }} = > Dec. 12, 2014, 5:03 a.m.

TEST CASE

    from datetime import datetime
    from dateutil.tz import tzutc, tzlocal

    utc = datetime.now(tzutc())
    print('UTC:   ' + str(utc))

    local = utc.astimezone(tzlocal())
    print('Local: ' + str(local))

I am getting correct datetime here. And when i use myobject.created_at.astimezone(tzlocal()) it also returns correct. but its when in Templates i get 1 hour ahead time. WHY? i tried both |local filter also but no use

What am I missing?

4
  • Is your server actually in the Chicago timezone? Commented Dec 12, 2014 at 11:46
  • Well i have defined the time zone in the settings.py as TIME_ZONE = 'America/Chicago'. i think this is default settings. Commented Dec 12, 2014 at 11:49
  • the 'TIME_ZONE` should reflect your actual time zone. ie the clock of the machine running django Commented Dec 12, 2014 at 12:34
  • Thats what is dynamic. When someone open the application in USA he would get the time accordingly like the one who open in UK. so i can not set it to any spcific time zone. getting? Commented Dec 12, 2014 at 14:44

5 Answers 5

11

Add the below snippet to the base.html

{% load tz %}

{% timezone "Asia/Kolkata" %}
  <All content goes here>
{% endtimezone %}

Inherit this to all templates. Everything render in 'Asia/Kolkata' time zone

Sign up to request clarification or add additional context in comments.

Comments

6

Django always stores data in UTC with timezone aware info. Your settings.py is for America/Chicago therefore template renders it accordingly.

You can use timezone filters in your template to always show local time {{ image.created_at | localtime }}. At template start do {% load tz %} timezone tag library.

I.e.

{% load tz %}

<!-- template -->

{{ image.created_at | localtime }}

<!-- more template -->

1 Comment

As per testcase code I get expected timezone in template, no variation of 1 hr. Please share exact template code.
3

From the docs:

localtime

Forces conversion of a single value to the current time zone.

For example:

{% load tz %}

{{ value|localtime }}

Comments

1

The TIME_ZONE setting is for the default (usually sever location timezone) to display time information related to the logged user you should read the documentation here

Comments

0

I found easy way to do it with custom template filter, please refer to this answer.

Before filter: 2022-09-15 17:56:26.936210 After filter -> 15.09.2022 20:56

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.