Django 日期格式'dd-mm-yyyy' [英] django date format 'dd-mm-yyyy'
本文介绍了Django 日期格式'dd-mm-yyyy'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有什么方法可以让 django 将我的数据作为dd-mm-yyyy"(如果需要)存储在 postgresql 中,并让 django 表单验证dd-mm-yyyy"?
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'?
(我尝试过但没有成功:
DATE_INPUT_FORMATS = ('%d-%m-%Y')
USE_I18N = True
USE_L10N = True
做了很多谷歌搜索但没有成功:(
推荐答案
问题是我需要 settings.py
文件中的 ISO 和 UK 日期格式,如下所示:
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')
然后将 forms.py 调整为以下内容:
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
为什么 USE_L10N = True
不能这样做,我不知道!
Why USE_L10N = True
can't just do this I don't know!
[感谢这个和
这篇关于Django 日期格式'dd-mm-yyyy'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文