gpt4 book ai didi

python - Django 日期过滤器 : how come the format used is different from the one in datetime library?

转载 作者:太空宇宙 更新时间:2023-11-04 11:03:52 24 4
gpt4 key购买 nike

要使用date 过滤器格式化日期,您必须使用以下格式:

{{ my_date|date:"Y-m-d" }}

如果您使用标准 datetime 中的 strftime,则必须使用以下内容:

my_date.strftime("%Y-%m-%d")

所以我的问题是......这不是很丑吗(我想这是因为 % 也用于标签,因此被转义了或什么的)?

但这不是主要问题......我想在整个项目的 settings.py 中使用相同的 DATE_FORMAT 参数化,但因此看来我不能 !是否有解决方法(例如,在日期格式化为 {{ my_date|date|dream_filter }} 之后删除 % 的过滤器,因为如果我只是使用DATE_FORMAT = "%Y-%m-%d" 我得到了类似 %2001-%6-%12) 的东西?

最佳答案

使用 DATE_INPUT_FORMATS。

from django.conf import settings
py_format = settings.DATE_INPUT_FORMATS[0]

关于python - Django 日期过滤器 : how come the format used is different from the one in datetime library?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3014615/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com