Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

python - Django datetime not validating right

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

The ModelForm class Meta: looks like the following:

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',
        })

    }

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?

EDIT FOR CLARIFICATION:

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The misconception:

To quote the docs:

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:

  • provide the form field (forms.DateTimeField) with the datetime format you would like to use by passing a input_formats argument

    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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...