Django DateInput 小部件格式
我试图在 DateInput 小部件中将日期格式从 mm/dd/yyyy 更改为 dd/mm/yyyy 。我正在尝试按以下方式更改格式。
class Meta:
model = Patient
fields = '__all__'
widgets = {
'date_of_birth': widgets.DateInput(
format='%d-%m-%Y',
attrs={'type': 'date', 'class': 'form-control'})
}
我注意到,如果我不包含 'type': 'date'
属性,则日期将以正确的格式解释,但在这种情况下,小部件不会显示但只是一个文本字段。所以你必须手动写日期(包括/)。使用看起来像这样的 DateInput 小部件,将 dd 和 mm 切换为 DateInput 小部件
我还尝试按以下方式更改 settings.py
,但这也没有帮助。
LANGUAGE_CODE = 'en-GB'
TIME_ZONE = 'CET'
USE_L10N = True
USE_TZ = True
DATE_INPUT_FORMATS = ('%d/%m/%Y')
I am trying to change the date format from mm/dd/yyyy to dd/mm/yyyy in a DateInput widget. I am trying to change the format the following way.
class Meta:
model = Patient
fields = '__all__'
widgets = {
'date_of_birth': widgets.DateInput(
format='%d-%m-%Y',
attrs={'type': 'date', 'class': 'form-control'})
}
I noticed that if I don't include the 'type': 'date'
attribute the date is interpreted in the correct format, but in that case the widget isn't displayed but just a text-field. So you have to write the date manually (including /). With the DateInput widget that looks like this with dd and mm switched DateInput widget
I have also tried changing the settings.py
the following way which also didn't help.
LANGUAGE_CODE = 'en-GB'
TIME_ZONE = 'CET'
USE_L10N = True
USE_TZ = True
DATE_INPUT_FORMATS = ('%d/%m/%Y')
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 settings.py 中设置 DATE_INPUT_FORMATS 如下:
在 ModelForm 中,您可以执行如下操作:
请记住,格式必须在 forms.py 中定义,而不是在 models.py 中定义。
In settings.py set DATE_INPUT_FORMATS as below:
And in your ModelForm you could do something like below:
keep in mind, that formats must be defined in the forms.py and not in the models.py.
问题出在我的电脑设置上。我将语言设置为英语(美国)。将语言更改为英语(英国)解决了该问题。
The issue was with my computer settings. I had the language set to English (US). Changing the language to English (UK) fixed the issue.