Django 日期格式“dd-mm-yyyy”

发布于 2024-10-15 11:56:40 字数 292 浏览 2 评论 0原文

有什么方法可以让 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

赠意 2024-10-22 11:56:40

问题原来是我需要在 settings.py 文件中同时使用 ISO 和 UK 日期格式,如下所示:

DATE_INPUT_FORMATS = ('%d-%m-%Y','%Y-%m-%d')

然后将 forms.py 调整为以下内容:

class ClientDetailsForm(ModelForm):
    date_of_birth = DateField(input_formats=settings.DATE_INPUT_FORMATS)
    class Meta:
        model = ClientDetails

为什么 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:

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]

你的往事 2024-10-22 11:56:40

settings.py 中的 DATE_INPUT_FORMATSUSE_I18N = True 没有影响。这是因为 django 会加载活动语言环境的特定格式。 Django 附带了许多不同语言环境的格式定义。

您可以覆盖 django 默认格式定义:

mysite/
    formats/
        __init__.py
        en/
            __init__.py
            formats.py

如 django 文档中所述: 创建自定义格式文件

DATE_INPUT_FORMATS in settings.py has no effect with USE_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:

mysite/
    formats/
        __init__.py
        en/
            __init__.py
            formats.py

As described in django documentation: Creating custom formats file

挽梦忆笙歌 2024-10-22 11:56:40

Sevenearths 答案 中有评论

为什么 USE_L10N = True 不能这样做,我不知道!

该文档说 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

Why USE_L10N = True can't just do this I don't know!

The documentation says that USE_L10N = True is the default setting, but to get UK dates to work the LANGUAGE_CODE must also be changed from its default setting of en-us to en-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.

獨角戲 2024-10-22 11:56:40

我使用了以下适用于添加和编辑表单案例的代码。

在settings.py中

DATE_INPUT_FORMATS = ('%d/%m/%Y','%d-%m-%Y','%Y-%m-%d')

在forms.py中

class MyModelForm(forms.ModelForm):
    issue_date = forms.DateField(widget=forms.DateInput(format = '%d/%m/%Y'), input_formats=settings.DATE_INPUT_FORMATS)

I used below code that worked on Both Add and Edit Form Case.

in settings.py

DATE_INPUT_FORMATS = ('%d/%m/%Y','%d-%m-%Y','%Y-%m-%d')

in forms.py

class MyModelForm(forms.ModelForm):
    issue_date = forms.DateField(widget=forms.DateInput(format = '%d/%m/%Y'), input_formats=settings.DATE_INPUT_FORMATS)
π浅易 2024-10-22 11:56:40

这可能不是确切问题的答案,但自从我来到这里...我建议使用日期小部件,并让 Django 以它理解的方式保存日期。在表单中定义字段,如下所示:

import datetime
from django import forms

def last_years():
    first_year = datetime.datetime.now().year - 6
    return list(range(first_year + 7, first_year, -1))

class MyForm(forms.Form):
    # This is it... it's user friendly, and can't go wrong parsing it
    date = forms.DateField(widget=forms.SelectDateWidget(years = last_years()))

如果将其放入 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:

import datetime
from django import forms

def last_years():
    first_year = datetime.datetime.now().year - 6
    return list(range(first_year + 7, first_year, -1))

class MyForm(forms.Form):
    # This is it... it's user friendly, and can't go wrong parsing it
    date = forms.DateField(widget=forms.SelectDateWidget(years = last_years()))

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?

寂寞花火° 2024-10-22 11:56:40

这对我有用

date_of_birth = forms.DateField(
    localize=True,
    widget=forms.DateInput(format = '%Y-%m-%d',attrs={'type': 'date'}),
)

This works for me

date_of_birth = forms.DateField(
    localize=True,
    widget=forms.DateInput(format = '%Y-%m-%d',attrs={'type': 'date'}),
)
深府石板幽径 2024-10-22 11:56:40

您需要设置 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 = ('%d-%m-%Y')
USE_I18N = True
USE_L10N = False # Here

文档在 DATE_INPUT_FORMATS

USE_L10NTrue时,区域设置指定的格式具有更高的优先级,并将被应用。

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 to DATE_INPUT_FORMATS so you need to set USE_L10N False:

DATE_INPUT_FORMATS = ('%d-%m-%Y')
USE_I18N = True
USE_L10N = False # Here

The doc says below in DATE_INPUT_FORMATS:

When USE_L10N is True, the locale-dictated format has higher precedence and will be applied instead.

淡写薰衣草的香 2024-10-22 11:56:40

我会覆盖 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/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文