2

TimeInput is not working for the form given below. But SelectDateWidget() works fine. No error is created for current TimeInput() widget.

Suggest the correct way to use TimeInput widget.

forms.py

class TournmentDetails(forms.ModelForm):

    class Meta():
        model = Tournament
        fields = ('eventname','venue','date','time')

        widgets = {
            'date': forms.SelectDateWidget(
                 empty_label=("Choose Year", "Choose Month", "Choose Day"),
            ),
            'time': forms.TimeInput(format='%H:%M'),
        }

models.py

class Tournament(models.Model):
    eventname = models.CharField(max_length=30, default="None", choices = EVENT_CHOICES)
    venue = models.CharField(max_length=30)
    date = models.DateField(max_length=30)
    time = models.TimeField(max_length=30)
VA splash
  • 553
  • 1
  • 8
  • 25

2 Answers2

9

By default the TimeInput widget renders as an html input field of type text:

<input type="text" name="time" id="id_time">

If, instead, you want to render it as an html input field of type time:

<input type="time" name="time" id="id_time">

then in the meta widgets section of your ModelForm class add a type attribute like this:

'time': forms.TimeInput(attrs={'type': 'time'})

User
  • 62,498
  • 72
  • 186
  • 247
0

I had the same problem with TimeInput().
I ended up following Vitor Freitas' tutorial and using Django Tempus Dominus, which includes a Time Picker option.

Hope this helps!

beasclo
  • 371
  • 2
  • 6
  • It is considered best practice to write out the answer here in case the link changes or goes dead. –  Aug 26 '19 at 00:58