Django forms.DateInput does not apply the attributes given in attrs field

前端 未结 4 1848
情书的邮戳
情书的邮戳 2020-12-31 18:52

Placeholder, class not getting set when tried to apply through the django\'s attrs specifier for forms.DateInput

The form is a ModelForm.

And according to th

4条回答
  •  無奈伤痛
    2020-12-31 19:16

    you can simply create a HTML Datepicker Without using a fancy JAvascript Library.Here is the simple demo:

    First create a dateinput field inherited from Dateinput:

     class DateInput(forms.DateInput):
         input_type = 'date'
    

    And use this DateInput Field as like this :

     class BookingRoomForm(ModelForm):
         class Meta:
             widgets = {'checkin':DateInput()}
             model = BookingRoom
             fields = ['checkin',]
    

    If you're not using ModelForm,Use widget something like this :

    class BookingRoomForm(ModelForm):
        checkin = forms.DateField(widget=DateInput())
        class Meta:
            model = BookingRoom
            fields = ['checkin',]
    

    And the output will be like this : enter image description here

提交回复
热议问题