python - 是否有将日期时间转换为 "5 hours ago"或 "12 days ago"的 Django 模板过滤器?

标签 python css django templates filter

假设我有一个日期时间。我这样做:

Submitted on {{ post.date|date:"D. M d, P" }}

这实际上打印了日期。但是,如果我想让它说“4 小时前”或“55 天前”或“2 个月前”怎么办?

最佳答案

试试模板过滤器 timesince .

像这样使用它:

{{ mytemplatevar|timesince }}

关于python - 是否有将日期时间转换为 "5 hours ago"或 "12 days ago"的 Django 模板过滤器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4582507/

相关文章:

python - 使用线程将 stdout 重定向到 Tkinter 文本小部件的问题

python - 为什么 ThreadPoolExecutor 比 for 循环慢?

html - 如何制作看起来不像按钮的 Glyphicon 按钮

php - 如何使用ajax显示值

python - 值错误 : invalid literal for int() with base 10: 'NULL'

python - 同时使用 Python f-strings 和 Jinja

python - 将文件转换为数字图像失败,出现 "tile cannot extend outside image"

javascript - jQuery Animate 在中间快速旋转和滚动文本但在结束时慢

python - 为什么在 Django 模型中使用 'self' 外键?

python - 类型错误 : cannot unpack non-iterable int object in Django views function