Date format for django's SelectDateWidget

本秂侑毒 提交于 2019-12-12 08:58:47

问题


SelectDateWidget is very convenient but it normally seems to return dates in the format "%Y-%m-%d". It doesn't take a format parameter and doesn't have much documentation. Anyone run into this or have an idea how to work around it to get the output of the date into another format? There is this ticket, #6231 saying that there's a fix to have it use a DATE_FORMAT setting from settings.py (though I don't see why it can't just be given a format parameter also). There's a patch in the ticket from 2 months ago to implement this, so it seems I would have to get the latest dev version of django to use it, unless there is a way I can apply just this patch to my django install?


回答1:


DATE_FORMAT works with SelectDateWidget but you have to use format strings without percents (as used in: now template tag) like this:

DATE_FORMAT = 'j N Y'



回答2:


Have you tried this setting yet ? According to the docs, it seems DATE_FORMAT= has been a valid setting since version 1.0. Are you using a pre-1.0 version of django?

http://docs.djangoproject.com/en/1.0/ref/settings/#date-format




回答3:


The correct way to do this is with spaces rather than forward slashes i.e.

DATE_FORMAT = "%d %m %Y"


来源:https://stackoverflow.com/questions/1554783/date-format-for-djangos-selectdatewidget

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