django日期格式'dd-mm-yyyy' [英] django date format 'dd-mm-yyyy'
本文介绍了django日期格式'dd-mm-yyyy'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有什么办法可以让django将postgresql中的数据存储为'dd-mm-yyyy'(如果需要),并且django形式对'dd-mm-yyyy'进行验证?
(我尝试过没有太多的成功,例如:
DATE_INPUT_FORMATS =('%d-%m-%Y')
USE_I18N = True
USE_L10N = True
做了很多谷歌搜索,但没有成功:(
解决方案
问题原因是我需要在 settings.py
文件中的ISO和UK日期格式,如下所示:
DATE_INPUT_FORMATS =('%d-%m-%Y','%Y-%m-%d')
pre>
然后将forms.py调整为以下内容:
类ClientDetailsForm(ModelForm):
date_of_birth = DateField(input_formats = settings.DATE_INPUT_FORMATS)
class Meta:
model = ClientDetails
为什么?
USE_L10N = True
不能这样做我不知道!
Is there any way I can get django to store my data in postgresql as 'dd-mm-yyyy' (if required) and have the django forms validate for 'dd-mm-yyyy'?
(I have tried without much success things like:
DATE_INPUT_FORMATS = ('%d-%m-%Y') USE_I18N = True USE_L10N = True
And done a lot of googling but no success :(
解决方案The problem turned out to be that I needed both the ISO and UK date formats in the
settings.py
file like so:DATE_INPUT_FORMATS = ('%d-%m-%Y','%Y-%m-%d')
and then the forms.py adjusted to the following:
class ClientDetailsForm(ModelForm): date_of_birth = DateField(input_formats=settings.DATE_INPUT_FORMATS) class Meta: model = ClientDetails
Why
USE_L10N = True
can't just do this I don't know!这篇关于django日期格式'dd-mm-yyyy'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文