how to show datepicker calendar on datefield

You can use widget to pass a class to the form when it will be rendered in the html. Then that class will be read by javascript to render the datepicker function.

Here is an example:

from django import forms
class HolidayTimeForm(forms.Form):
    holiday_date = forms.DateField(widget=forms.TextInput(attrs=
                                {
                                    'class':'datepicker'
                                }))

… or through the widgets attribute in the Meta class when using ModelForm:

class HolidayTimeForm(forms.ModelForm):

    class Meta:
        model = Holiday
        widgets = {
            'holiday_date': forms.DateInput(attrs={'class':'datepicker'}),
        }

Now in template:

 /* Include the jquery Ui here */
 <script>
  $(function() {
    $( ".datepicker" ).datepicker({
      changeMonth: true,
      changeYear: true,
      yearRange: "1900:2012",
      // You can put more options here.

    });
  });
  </script>

Leave a Comment