Python 如何在django中使用本地时间设置呈现时区感知模型字段

Python 如何在django中使用本地时间设置呈现时区感知模型字段,python,django,Python,Django,假设我有以下代码 # settings.py in a proper location ... TIME_ZONE = 'Asia/Seoul' USE_TZ = True # models.py in a proper location class Post(models.Model): created = models.DateTimeField(auto_now_add=True) def created_formatted(self): return

假设我有以下代码

# settings.py in a proper location
...
TIME_ZONE = 'Asia/Seoul'
USE_TZ = True


# models.py in a proper location
class Post(models.Model):
    created = models.DateTimeField(auto_now_add=True)
    def created_formatted(self):
        return self.created.strftime("%Y/%m/%d %H:%M")
然后,我将以下django模板呈现为html

before formatting: {{ a_post.created }}  # localtime
after formatting: {{ a_post.created_formatted }}  # not localtime
我希望
{{a_post.created}}
{{a_post.created_formated}}
具有相同的时区,但它们没有。我试图找出如何将
{{a_post.created}}
呈现到localtime,但我不确定django代码的哪一部分是正确的查找位置。(如果您让我知道django源代码中要查找的正确部分,这将非常有帮助)。在我找到django模板中datetime的呈现方式之后,我想将同样的想法应用到我创建的_格式中

p、 我知道。但是,对于我拥有的每一种方法,我真的必须手动执行吗?任何建议都会有帮助

来自文档

{% load tz %}

{% localtime on %}
    {{ value }}
{% endlocaltime %}

{% localtime off %}
   {{ value }}
{% endlocaltime %}
如果你需要一个特定的时区

{% load tz %}

{% timezone "Europe/Paris" %}
    Paris time: {{ value }}
{% endtimezone %}

{% timezone None %}
    Server time: {{ value }}
{% endtimezone %}
文件, 来自文档的

文档

{% load tz %}

{% localtime on %}
    {{ value }}
{% endlocaltime %}

{% localtime off %}
   {{ value }}
{% endlocaltime %}
如果你需要一个特定的时区

{% load tz %}

{% timezone "Europe/Paris" %}
    Paris time: {{ value }}
{% endtimezone %}

{% timezone None %}
    Server time: {{ value }}
{% endtimezone %}
文件,
我真是太蠢了。就在时区文件上。非常感谢。现在看来{a_post.created}}的localtime标记是打开的,但{a_post.created_formated}的localtime标记不是打开的。你知道为什么会这样吗?如果您能让我知道django代码的哪一部分对此负责,那将更有帮助…-)哇,我真傻。就在时区文件上。非常感谢。现在看来{a_post.created}}的localtime标记是打开的,但{a_post.created_formated}的localtime标记不是打开的。你知道为什么会这样吗?如果您能让我知道django代码的哪一部分对此负责,那将更有帮助…-)