Django datetime未验证正确 [英] Django datetime not validating right

查看:114
本文介绍了Django datetime未验证正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用HTML5 datetime-local 输入类型来尝试将一些datetime数据导入我的数据库。

I'm using the HTML5 datetime-local input type to try and get some datetime data into my database.

ModelForm 类元:如下所示:

class Meta:
    model = ScheduleEntry
    fields = ['calendar', 'title', 'start', 'end', 'assets', 'users']

    widgets = {
        'calendar': forms.Select(attrs={
            'class': 'fom-control chosen-select'
        }),
        'title': forms.TextInput(attrs={
            'class': 'form-control'
        }),
        'start': forms.DateTimeInput(attrs={
            'type':'datetime-local',
            'class':'form-control'
        }, format='%Y-%m-%dT%H:%M'),
        'end': forms.DateTimeInput(attrs={
            'type': 'datetime-local',
            'class': 'form-control'
        }, format='%Y-%m-%dT%H:%M'),
        'assets': forms.SelectMultiple(attrs={
            'class': 'form-control chosen-select'
        }),
        'users': forms.SelectMultiple(attrs={
            'class': 'form-control chosen-select',
        })

    }

我一直无法通过表单验证,这导致我不敢理hair。这是文档页面,显示它应该可以工作,但是好像我遗漏了某些东西?

I keep failing on form validation and it's causing me to pull my hair out.This is the documentation page that shows it should work, but it looks like I'm missing something?

编辑为澄清起见:

该错误消息同时显示 start end ,它是输入有效的日期/时间

The error message is for both start and end and it's Enter a valid date/time

推荐答案

误解



引用文档


窗口小部件不应与表单字段融合。表单字段处理输入验证的逻辑,并直接在模板中使用。窗口小部件处理HTML表单输入元素在网页上的呈现和原始提交数据的提取。

Widgets should not be confused with the form fields. Form fields deal with the logic of input validation and are used directly in templates. Widgets deal with rendering of HTML form input elements on the web page and extraction of raw submitted data.

窗口小部件对验证没有影响。您为窗口小部件提供了格式参数,但这并不意味着表单字段验证将使用它-它仅设置窗口小部件内容呈现的初始格式:

Widgets have no influence on validation. You gave your widgets a format argument, but that does not mean the form field validation will use it - it only sets the initial format the widget's content is rendered with:


格式:此字段的初始值的显示格式。

format: The format in which this field’s initial value will be displayed.






解决方案



两个选项:


The solutions

Two options:

class MyIdealDateForm(forms.ModelForm):
    start = forms.DateTimeField(
        input_formats = ['%Y-%m-%dT%H:%M'],
        widget = forms.DateTimeInput(
            attrs={
                'type': 'datetime-local',
                'class': 'form-control'},
            format='%Y-%m-%dT%H:%M')
    )

这需要为每个表单字段(甚至可能是扩展名,甚至是其小部件)完成。您在这里所做的是有效地覆盖设置(请参见下一点)。

This needs to be done for every form field (and probably by extension even their widgets). What you are doing here is effectively overwriting the settings (see the next point).

将日期时间格式添加到设置作为第一项。这将全局应用于所有使用该设置的表单域和小部件。

Add your datetime format to the settings as the first item. This will apply globally to all formfields and widgets that use that setting.

这篇关于Django datetime未验证正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆