Problems using SplitDateTimeWidget

Hello!

I’m trying to use SplitDateTimeWidget in my forms. Problem is the widget is not showing my inital date, only the time as follows:

Captura de tela de 2023-03-13 10-33-32

Here is my field Form

    end_date= forms.SplitDateTimeField(
        label=_('End Date'),
        widget=forms.SplitDateTimeWidget(
        date_attrs={
            'type':'date'
            },
        date_format='%d/%m/%Y',
        time_attrs={
            'type':'time'
            },
        time_format='%H:%M',
        )
    )

Is this a model form?

If so

  • What does the rest of the Meta class for that form look like?
  • What does the model look like?
  • What does the view look like?

If not

  • What does the view look like?

Do you have an __init__ method defined for your form? If so, what does it look like?

Hi @KenWhitesell!

Yes, it is a model form.

Meta class:

Form meta class:
 class Meta:
        model = CalendarEvent
        fields = [
              'other_fields',
              'end_date
                  ]

Model:

    end_date= models.DateTimeField(
        verbose_name=_('End Date')
    )

View:

def agenda_evento_view(request):

	event=request.POST.get('event', None)

	e= AgendaEvento.objects.filter(id=evento).first()
	form=EventoForm(instance=e)


	return render(
		request,
		'agenda/evento.html',
		context={
		'form':form
		}
	)

__init__:

    def __init__(self, *args, **kwargs):
        super(EventForm, self).__init__(*args, **kwargs)

        if 'end_date' in self.fields:
            self.fields['end_date'].initial = self.instance.end_date

You’ve defined your form for being for the CalendarEvent model but are populating it with an instance of AgendaEvento. A translation issue perhaps?

If this is intentional, which model is that end_date field in? And what’s the field definition for end_date in the other model?

This shouldn’t be necessary. If you’re not getting the initial value from the instance, that’s a sign that something else is going on here.

Also, examine the actual html that was rendered and sent to the browser. (You can check this in your browser’s developer tools.) I would expect you to see something like this:

<input type="date" name="end_date_0" value="13/03/2023" required id="id_end_date_0"><input type="time" name="end_date_1" value="14:24" required id="id_end_date_1">

Yes, is a translation issue, sorry about that. Please assume AgendaEvento as CalendarEvent and EventoForm as EventForm.

I agree, this is not necessary, but I was doing it hoping it would help somehow.

It renders as expected:
<input type="date" name="end_date_0" value="02/03/2023" required="" id="id_end_date_0">

No worries, no need to apologize - I appreciate the difficulties with trying to work in a non-primary language. I just wanted to clarify the situation.

The html is rendering correctly. This means that the issue is in the browser and not in Django.

That implies to me that you have some JavaScript that is supposed to be rendering that widget in the browser and isn’t working or possibly not configured correctly.
Do you have a specific JavaScript enhancement that you are using for this?

With this widget I’m not using any JavaScript. Could be a problem with Firefox? I’ll test with other browser and see what happens.

Hi @KenWhitesell !

Me and my coworker figured it out.

Apparently the SplitDateTimeWidget argument date_format has to receive a specific date pattern to work, as follows:

date_format='%Y-%m-%d'

Your hint about JavaScript lead us to find the answer.
I’ll mark it as solved, thanks!