日期选择器未显示

时间:2017-06-03 11:19:19

标签: django python-3.x datepicker django-forms django-templates

我是前瞻性发展的新手。通常我做后端和infra。在白天,我听到frontend-angular人在datepicker小部件上谈了很多话。现在我从日常工作中解脱出来并拥有他的示例源代码来学习。我想首先开始一个最容易的,然后回到我好奇心的根源:)

how to show datepicker calendar on datefield

我正在尝试将datepicker添加到我的页面,用验证器替换普通文本字段:

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")

模板

<!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>

我必须误解此页面中的内容。

1 个答案:

答案 0 :(得分:1)

您可能需要加载JQuery-UI

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

(如this answer

如果你还没有JQuery