# Problems using SplitDateTimeWidget

**URL:** <https://forum.djangoproject.com/t/problems-using-splitdatetimewidget/19473>\
**Category:** Forms & APIs\
**Created:** [March 13, 2023, 1:35pm UTC](https://forum.djangoproject.com/t/problems-using-splitdatetimewidget/19473 "2023-03-13T13:35:47Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![obarbachan](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/obarbachan/32/10603_2.png) [@obarbachan](https://forum.djangoproject.com/u/obarbachan)\
**Post date:** [March 13, 2023, 1:35pm UTC](https://forum.djangoproject.com/t/problems-using-splitdatetimewidget/19473/1 "2023-03-13T13:35:47Z")

</div>

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](https://us1.discourse-cdn.com/flex026/uploads/djangoproject/original/2X/a/a842522665126c8cbcedc4cb18d06989ba886f7f.png)

Here is my field Form

```auto
    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',
        )
    )

```

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [March 13, 2023, 1:54pm UTC](https://forum.djangoproject.com/t/problems-using-splitdatetimewidget/19473/2 "2023-03-13T13:54:59Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![obarbachan](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/obarbachan/32/10603_2.png) [@obarbachan](https://forum.djangoproject.com/u/obarbachan)\
**Post date:** [March 13, 2023, 2:10pm UTC](https://forum.djangoproject.com/t/problems-using-splitdatetimewidget/19473/3 "2023-03-13T14:10:39Z")

</div>

> [@KenWhitesell](#):
>
> `Meta`

Hi @KenWhitesell!

Yes, it is a model form.

Meta class:

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

```

Model:

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

```

View:

```auto
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__ `:

```auto
    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

```

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [March 13, 2023, 2:25pm UTC](https://forum.djangoproject.com/t/problems-using-splitdatetimewidget/19473/4 "2023-03-13T14:25:02Z")

</div>

> [@obarbachan](#):
>
> ```auto
> model = CalendarEvent
> 
> ```

> [@obarbachan](#):
>
> ```auto
> e= AgendaEvento.objects.filter(id=evento).first()
> form=EventoForm(instance=e)
> 
> ```

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?

> [@obarbachan](#):
>
> ```auto
> if 'end_date' in self.fields:
> self.fields['end_date'].initial = self.instance.end_date
> 
> ```

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:

```auto
<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">

```

---

<div class="post-metadata">

**Author:** ![obarbachan](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/obarbachan/32/10603_2.png) [@obarbachan](https://forum.djangoproject.com/u/obarbachan)\
**Post date:** [March 13, 2023, 2:45pm UTC](https://forum.djangoproject.com/t/problems-using-splitdatetimewidget/19473/5 "2023-03-13T14:45:14Z")

</div>

> [@KenWhitesell](#):
>
> You’ve defined your form for being for the `CalendarEvent` model but are populating it with an instance of `AgendaEvento`. A translation issue perhaps?

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

> [@KenWhitesell](#):
>
> .

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">`

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [March 13, 2023, 2:58pm UTC](https://forum.djangoproject.com/t/problems-using-splitdatetimewidget/19473/6 "2023-03-13T14:58:23Z")

</div>

> [@obarbachan](#):
>
> Yes, is a translation issue, sorry about that.

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.

> [@obarbachan](#):
>
> `<input type="date" name="end_date_0" value="02/03/2023" required="" id="id_end_date_0">`

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?

---

<div class="post-metadata">

**Author:** ![obarbachan](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/obarbachan/32/10603_2.png) [@obarbachan](https://forum.djangoproject.com/u/obarbachan)\
**Post date:** [March 13, 2023, 3:14pm UTC](https://forum.djangoproject.com/t/problems-using-splitdatetimewidget/19473/7 "2023-03-13T15:14:46Z")

</div>

> [@KenWhitesell](#):
>
> 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.

---

<div class="post-metadata">

**Author:** ![obarbachan](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/obarbachan/32/10603_2.png) [@obarbachan](https://forum.djangoproject.com/u/obarbachan)\
**Post date:** [March 13, 2023, 5:33pm UTC](https://forum.djangoproject.com/t/problems-using-splitdatetimewidget/19473/8 "2023-03-13T17:33:51Z")

</div>

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:

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

```

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