Django时区本地化不能按预期工作 [英] Django timezone localization not working as expected

查看:180
本文介绍了Django时区本地化不能按预期工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是Django 1.4.3和Postgres 9.1.3。这是我的模板,其中 message.created_at 是一个python datetime 对象,它清楚地告诉我datetime对象被存储在GMT中,我可以通过日期过滤器中的 e 进行调试。转换到本地时间是IST并没有发生,尽管我使用了 docs 。它仍然以GMT格式显示日期时间值。我缺少什么?

  {%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%}

尝试另一种方法,

  {%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>

这是我的设置,

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


解决方案

好的。考虑到这些帖子帖子#1 post#2 我将从用户手动获取时区,注册并存储在数据库中。并将与 timezone.activate(request.user.timezone)


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

解决方案

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

这篇关于Django时区本地化不能按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆