4

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)
2
  • 1
    What do you mean by "is not working"? Whats the current and expected behaviour? Commented Mar 28, 2019 at 18:26
  • Hour and minutes dropdown menu. Commented Mar 28, 2019 at 23:37

2 Answers 2

11

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

Sign up to request clarification or add additional context in comments.

Comments

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!

1 Comment

It is considered best practice to write out the answer here in case the link changes or goes dead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.