python - 如何在 django 的 python 中将日期时间转换为字符串

标签 python django

我的模型中有一个日期时间对象。 我将它发送到 View ,但在 html 中我不知道要写什么来格式化它。

我在努力

{{ item.date.strftime("%Y-%m-%d")|escape }}

但是我明白了

TemplateSyntaxError: Could not parse some characters: item.date.strftime|("%Y-%m-%d")||escape

刚用的时候

{{ item.date|escape }}

它正在工作,但现在是我想要的格式。

有什么建议吗?

最佳答案

尝试使用内置的 Django date format filter相反:

{{ item.date|date:"Y M d" }}

关于python - 如何在 django 的 python 中将日期时间转换为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/794995/

相关文章:

django - Matplotlib - Django 中的 Mpld3 Fig_to_html()

python - 高级切片。高阶切片/选择

python - 从当前工作目录开始获取类的文件路径

python - 从行索引的数据帧索引数据帧

Django,ModelChoiceField() 和初始值

django - django rest 框架序列化程序中的 id 字段

python - 在 VS Code 中选择 python 解释器时出现错误 : spawn ENOTDIR,

python - 为什么 Queue().get() 可以停止(或中断) while true 循环?

python - 被tornado.concurrent.Future异常困惑

python - 从 Django South 的重复迁移中恢复