Django - extend date template filter

眉间皱痕 提交于 2021-01-29 04:14:06

问题


I am wondering if I can just define a variable like LONG_DATETIME_FORMAT = 'D d F Y H:i' in my settings and use it in template like: {{ my_date|date:"LONG_DATETIME_FORMAT" }}? Can this be done without overwritting default date filter? Or should I write custom filter?


回答1:


From the docs

The format passed can be one of the predefined ones DATE_FORMAT, DATETIME_FORMAT, SHORT_DATE_FORMAT or SHORT_DATETIME_FORMAT, or a custom format that uses the format specifiers shown in the table above. Note that predefined formats may vary depending on the current locale.

So feel free to set any of them in the settings

DATETIME_FORMAT = 'D d F Y H:i'

{{ my_date|date:"DATETIME_FORMAT" }}


来源:https://stackoverflow.com/questions/35408993/django-extend-date-template-filter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!