django模板表列中不支持日期选择器

时间:2018-04-17 13:14:39

标签: django datepicker

表单字段的日期选择器,放在模板中不支持..这是代码

forms.py:

class GuestFacultyCourseOfferForm(BaseGuestFacultyCourseOfferForm):

        teaching_mode = forms.ModelChoiceField(queryset=TeachingMode.objects.all())
        location_mode = forms.ModelChoiceField(queryset=LocationMode.objects.all())
        # class_start_date = forms.DateField(required=True,)
        classstartdate = forms.DateField(label='Date',required=True)

模板代码:  这是日期选择器的脚本代码,它是为表单字段“classstartdate”提供的。

<script type="text/javascript" src="{% url 'admin:jsi18n' %}"></script>
<link rel="stylesheet" href="https://code.jquery.com/ui/1.11.0/themes/smoothness/jquery-ui.css"> 
<script type="text/javascript" src="{% static 'admin/js/jquery-new.js' %}"></script>
<script type="text/javascript" src="{% static 'admin/js/jquery-ui.min.js' %}"></script>
<script type="text/javascript" src="{% static 'admin/js/jquery-validate.js' %}"></script>

<script>
  $(document).ready(function() {
    $( "#id_classstartdate" ).datepicker({
      changeMonth: true,
      changeYear: true,
      minDate:0,
      buttonImage: "{% static "admin/img/icon_calendar.gif" %}",
      buttonImageOnly: true,
      buttonText: "Select date",      
      showOn:"both",

    });
  });
  </script>

- &GT;这是我在表格中给出表单字段的正文代码:

<form  method="post">
{% csrf_token %}
<table id="formset" class="form">
  <thead>
    <tr>
      <th>TEACHING MODE</th>
      <th>LOCATION MODE</th>
      <th>CLASS START DATE</th>

    </tr>
  </thead>
{% for form in formset_assign_faculty.forms %}
  <tr class="{% cycle row1,row2 %}">
        <td>{{ form.teaching_mode}} {{ form.teaching_mode.errors.as_ul }}</td>
    <td>{{ form.location_mode}} {{ form.location_mode.errors.as_ul }}</td>
    <td>{{ form.classstartdate}} {{ form.location_mode.errors.as_ul }}</td>

  </tr>
{% endfor %}
</table>

</form>

1 个答案:

答案 0 :(得分:1)

如果您使用formset,则每个表单的输入ID不同,它形成为“id_”+表单前缀+字段名称。

因此,请尝试将发起datepicker的classtartdate更改为/date。这将找到以“id_”开头并以表单字段名/tag结尾的输入。