0

I have a datetimefield, which I am inputting data using the following form field:

delivery_pickup = forms.DateTimeField(label = "Delivery or Pickup Appointment", 
                                    required = False,
                                    widget=forms.DateTimeInput(attrs={'type':'datetime-local'}))

A nice little datetime widget appears automatically due to the type attribute declaration. The problem is when I try to submit using the widget, I get an error, and when I remove the widget and look at the input as a simple string, it includes a random T in between the date and the time, like so: 2021-09-26T13:17

Removing the T manually causes the error to go away. What's the best way to remove the T? Is it by formatting the string within the input field? Creating a clean method? Some other technique? Any help is appreciated.

EDIT: Evidently, the type declaration of 'datetime-local' is what causes it to be formatted it into an isot datetime string: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toISOString

The above article also gives a javascript function for converting the date to an ISO string: Date.toISOString() (2nd edit: actually, this also defaults to isot not iso format...)

The issue is I don't know how it interacts with the Django object. Can I call a JS function from inside of a form field or a widget? probably not, it would need to reside in the HTML for the page. The problem is that for my forms, I have simply looped over the form fields like so:

            {% for field in form %}
            <tr>    
                <td class="column_name">{{field.label}}</td>
                <td class="column_data">{{field}}</td>
                <td class="column_data">{{field.errors}}</td>
            </tr>
            {% endfor %}

Which means I haven't distinguished the singular one for datetime. So I'm at a bit of a standstill, appreciate any help on this last point

Fomorian
  • 25
  • 1
  • 5
  • It's not random. It's called isot format and is actually same as iso format only has a T instead of whitespace between date and time part. Astronomers like it. Take a look at time formats here: http://stsdas.stsci.edu/nadia/docs/_build/html/time/index.html#time-format – MSH Sep 26 '21 at 22:14
  • Perfect, just what I was looking for, thanks. My only question would be why does it default to isot format? I have not specified it in the model, nor the form field, nor even the widget. Nothing in the django documentation about defaulting to that format either... in fact it appears that the default should be [iso format](https://docs.djangoproject.com/en/3.2/ref/settings/#datetime-input-formats). Do you know whether I need to change the format in the widget, the form field, or the model? – Fomorian Sep 26 '21 at 22:38
  • It depends on if you want to define a storage or a display format. I would recommend the second option and change template's properties – Christophe Sep 26 '21 at 23:43

1 Answers1

0

This solution solved it: Django datetime not validating right

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

Basically, the input_formats inside the field and format argument inside the widget were required to agree before the form could be validated... Now I need the past few hours of my life back

Fomorian
  • 25
  • 1
  • 5