Django timezone localization not working as expected

青春壹個敷衍的年華 提交于 2020-01-07 02:44:07

问题


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

回答1:


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




回答2:


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).



来源:https://stackoverflow.com/questions/15065338/django-timezone-localization-not-working-as-expected

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!