Django timezone localization not working as expect

2019-09-06 08:24发布

I'm using Django 1.4.3 and Postgres 9.1.3. Here is my template where message.created_at is a python datetime object and it clearly tells me that the datetime object is stored in GMT as I can debug by passing e in date filter. The conversion to my local time which is IST is not happening though I used the block and filter given in the docs. It still renders the date time value in GMT. What am I missing?

{% load tz %}
{% localtime on %}
<div class="m_td_block">
    <span>{{ message.created_at|date:"D, d M, Y e" }}</span>
    <span>{{ message.created_at|time:"h:i A" }}</span>
</div>
{% endlocaltime %}

Tried the another approach too,

{% load tz %}
<div class="m_td_block">
    <span>{{ message.created_at|localtime|date:"D, d M, Y e" }}</span>
    <span>{{ message.created_at|localtime|time:"h:i A" }}</span>
</div>

And this is my settings,

TIME_ZONE = 'GMT'
USE_I18N = True
USE_L10N = True
USE_TZ = True

2条回答
Emotional °昔
2楼-- · 2019-09-06 08:49

Okay. Considering these posts post#1, post#2 I'm gonna get the timezone manually from the user while signup and store it in database. And will go with timezone.activate(request.user.timezone).

查看更多
仙女界的扛把子
3楼-- · 2019-09-06 08:53

Are you doing an activate to activate the local time-zone? See this.

查看更多
登录 后发表回答