Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何从数据库中存储的UTC日期时间在django模板中显示本地日期时间?_Python_Django_Datetime_Django Templates - Fatal编程技术网

Python 如何从数据库中存储的UTC日期时间在django模板中显示本地日期时间?

Python 如何从数据库中存储的UTC日期时间在django模板中显示本地日期时间?,python,django,datetime,django-templates,Python,Django,Datetime,Django Templates,我想在DJango模板中显示访客本地时区中的日期时间,从数据库中以UTC格式存储的日期时间开始。为此,我在不同的文件中做了以下更改 设置.py TIME_ZONE = 'UTC' USE_I18N = True USE_L10N = True USE_TZ = True page.html {% load tz %} <div> {{ start_time | localtime }} </div> and <div> {% localt

我想在DJango模板中显示访客本地时区中的日期时间,从数据库中以UTC格式存储的日期时间开始。为此,我在不同的文件中做了以下更改

设置.py

TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
page.html

{% load tz %}
<div>
    {{ start_time | localtime }}
</div>

and

<div>
    {% localtime on %} 
        {{ start_time }}
    {% endlocaltime %}
</div>
{%load tz%}
{{start_time}localtime}
和
{%localtime on%}
{{开始时间}
{%endlocaltime%}
由于此
start\u time
仅以字符串格式存储“HH:MM”。但我也尝试过日期时间对象。但这对我还是不起作用

还有一个问题,当我使用
datetime.utcnow()
datetime.now()
时,它们都在views.py中为我提供了“UTC”日期时间

或者建议我,如果有其他方法可以达到同样的效果

有谁能帮我解决这个问题吗? 任何帮助都将不胜感激

谢谢。

您必须使用django:

你必须使用django:


当我打印以查看输出时,它还为我提供UTC时间。当我打印以查看输出时,它为我提供UTC时间。尝试将settings.py中的语言代码更改为您的本地语言代码。尝试将settings.py中的语言代码更改为您的本地语言代码。
from django.utils import timezone

now = timezone.now()