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
并且进行了很多谷歌搜索,但没有成功:(
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 :(
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
问题原来是我需要在
settings.py
文件中同时使用 ISO 和 UK 日期格式,如下所示:然后将 forms.py 调整为以下内容:
为什么
USE_L10N = True我不知道不能这样做!
[感谢此和此]
The problem turned out to be that I needed both the ISO and UK date formats in the
settings.py
file like so:and then the forms.py adjusted to the following:
Why
USE_L10N = True
can't just do this I don't know![Thanks to this and this]
settings.py 中的
DATE_INPUT_FORMATS
对USE_I18N = True
没有影响。这是因为 django 会加载活动语言环境的特定格式。 Django 附带了许多不同语言环境的格式定义。您可以覆盖 django 默认格式定义:
如 django 文档中所述: 创建自定义格式文件
DATE_INPUT_FORMATS
in settings.py has no effect withUSE_I18N = True
. This is because django will load specific format for active locale. Django ships with format definitions for many different locales.You can override django default format definitions:
As described in django documentation: Creating custom formats file
在 Sevenearths 答案 中有评论
该文档说
USE_L10N = True
是默认设置,但要让英国日期正常工作LANGUAGE_CODE
还必须从其默认设置en-us
更改为en- GB
。更改后,日期字段会自动接受格式“dd/mm/yyyy”和“yyyy-mm-dd”(可能还有其他格式,我没有全部测试过),无需设置 input_formats = settings.DATE_INPUT_FORMATS< /code> 在每个表单日期字段中。
In Sevenearths answer there's the comment
The documentation says that
USE_L10N = True
is the default setting, but to get UK dates to work theLANGUAGE_CODE
must also be changed from its default setting ofen-us
toen-GB
.Once changed, date fields automatically accept the formats "dd/mm/yyyy" and "yyyy-mm-dd" (and possibly others, I've not tested them all) without needing to set
input_formats = settings.DATE_INPUT_FORMATS
in every form date field.我使用了以下适用于添加和编辑表单案例的代码。
在settings.py中
在forms.py中
I used below code that worked on Both Add and Edit Form Case.
in settings.py
in forms.py
这可能不是确切问题的答案,但自从我来到这里...我建议使用日期小部件,并让 Django 以它理解的方式保存日期。在表单中定义字段,如下所示:
如果将其放入 Django 模型的日期字段中,那么剩下的唯一问题就是格式化输出表示形式。我说得对吗?
This might not be the answer to the exact question, but since I got here... I would suggest using a date widget, and let Django save the date the way it understands it. Define the field in your form like so:
If you got it into a date field of the Django Model, then the only problem left would be format the output representation. Am I right?
这对我有用
This works for me
您需要设置 USE_L10N
False 如下所示,使 DATE_INPUT_FORMATS 工作更改 DateTimeField() 和 < 的日期格式a href="https://docs.djangoproject.com/en/4.2/ref/models/fields/#datefield" rel="nofollow noreferrer">DateField()。 *
USE_L10N
早于DATE_INPUT_FORMATS
,因此您需要设置USE_L10N
False
:文档在 DATE_INPUT_FORMATS:
You need to set USE_L10N
False
as shown below to make DATE_INPUT_FORMATS work to change the date format of DateTimeField() and DateField(). *USE_L10N
is prior toDATE_INPUT_FORMATS
so you need to setUSE_L10N
False
:The doc says below in DATE_INPUT_FORMATS:
我会覆盖 DateTimeField 来放置我的逻辑。 postgresql 如何存储它并不重要,你可以按照你想要的方式格式化它。
文件:
http://docs.djangoproject.com/en/dev/ref/models/实例/
I would overwrite the DateTimeField put my our logic. It doesn't matter how postgresql is storing it, you can format it how ever you want.
Docs:
http://docs.djangoproject.com/en/dev/ref/models/instances/