Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 328
robotHamster Avatar asked Nov 06 '18 21:11

robotHamster


People also ask

How do I check if a date is correct in Python?

Method #1 : Using strptime() In this, the function, strptime usually used for conversion of string date to datetime object, is used as when it doesn't match the format or date, raises the ValueError, and hence can be used to compute for validity.

What is the Django DateTimeField format?

DateTimeField in Django Forms is a date field, for taking input of date and time from user. The default widget for this input is DateTimeInput. It Normalizes to: A Python datetime. datetime object.


1 Answers

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.

like image 108
CoffeeBasedLifeform Avatar answered Sep 22 '22 23:09

CoffeeBasedLifeform