获取正确的本地时区以在Django Web应用程序中显示给最终用户 [英] Getting correct local time zone to display for end-users in Django web app

查看:78
本文介绍了获取正确的本地时区以在Django Web应用程序中显示给最终用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Django的初学者。我的settings.py文件中的时区设置为 UTC。我的网络应用的最终用户来自印度和墨西哥-两个截然不同的时区。我有一个评论模板,在其中显示这样的评论:

I'm a beginner in Django. Time zone in my settings.py file is set to 'UTC'. End users of my web app hail from India and Mexico - two very different time zones. I have a comments template where I display comments like so:

{% get_comment_list for object as comment_list %}
    {% for comment in comment_list reversed %}
    <p>
    <div class="comment">
    <a href="{% url 'profile' slug=comment.user_name %}">{{ comment.user_name }}</a>
      {{ ':' }}
      {{ comment.comment }}
      </div>
     <div class="date">
     {{ comment.submit_date }}
     </div>
     </p>
    {% endfor %}

我尝试过 {%在顶部加载tz%} ,然后尝试通过 {%localtime on%} 将comment.submit_date转换为本地时间,但它只会弹出为我选择UTC。

I've tried {% load tz %} at the top and then trying to convert comment.submit_date to local time via {% localtime on %}, but it merely spouts out UTC for me.

我如何确保我在印度和墨西哥的用户发表评论时看到他们的本地时间戳?

How do I make sure my users in India and Mexico see their local timestamps when they comment?

推荐答案

使用{%localtime on%}是正确的,但是您需要先激活用户的时区,然后才能使用它。 Django不会自动知道您用户的tz。

the use of {% localtime on %} is correct, but you need to activate your user's timezone before you can use that. Django does not automatically knows your user's tz.

https://docs.djangoproject.com/en/1.8/ref/utils/#django.utils.timezone.activate

# py
settings.TIME_ZONE = 'UTC'
timezone.activate('us/estern')

# template
{% localtime on %}
{{ datetime }} <= this time will be in estern standard time

这篇关于获取正确的本地时区以在Django Web应用程序中显示给最终用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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