2

I am new to frontend development. Normally I do backend and infra. During the daytime I heard from the frontend-angular guys talking on datepicker widget a lot. Now I am free from the routine job and have his example source code to study. I would like to start an easiest one first then back to the root source of my curiosity :)

how to show datepicker calendar on datefield

I am trying to add datepicker to my page replace ordinary textfield with validator:

from django import forms


class ReportOneForm(forms.Form):
    start_date = forms.DateField(widget=forms.TextInput(attrs={'class': 'datepicker'}))
    end_date = forms.DateField(widget=forms.TextInput(attrs={'class': 'datepicker'}))

    def clean(self):
        cleanded_data = super().clean()
        start_date = cleanded_data.get('start_date')
        end_date = cleanded_data.get('end_date')

        if start_date > end_date:
            raise forms.ValidationError(f"Start Date must not after End Date")

Template

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Report 1 Result</title>
    <form method="post" class="post-form">{% csrf_token %}
        {{ form.as_p }}
        <button type="submit" class="save btn btn-default">Save</button>
    </form>
</head>
<body>

</body>
</html>
<script>
  $(function() {
    $( ".datepicker" ).datepicker({
      changeMonth: true,
      changeYear: true,
      yearRange: "1900:2012",
      // You can put more options here.

    });
  });
</script>

I must misunderstand something in this page.

user2314737
  • 27,088
  • 20
  • 102
  • 114
joe
  • 8,383
  • 13
  • 61
  • 109

1 Answers1

1

You might need to load JQuery-UI

<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"></script>

(as in this answer)

And in case you don't have it also JQuery

user2314737
  • 27,088
  • 20
  • 102
  • 114
  • Thank you very much. The answer I found is 4 years old. Yours is ~3 years old which is more up to date – joe Jun 03 '17 at 11:37