且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

django日期格式'dd-mm-yyyy'

更新时间:2023-02-26 13:57:14

问题原因是我需要在 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!

[Thanks to this and this]