Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use TimeInput widget in Django forms?

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)
like image 797
VA splash Avatar asked Oct 21 '25 06:10

VA splash


1 Answers

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

like image 68
User Avatar answered Oct 22 '25 21:10

User