SelectDateWidget不工作

时间:2014-05-27 15:47:23

标签: django django-forms

在下面的代码中,我尝试将SelectDateWidget用于日期输入,但是它没有按预期应用于hire_date。我已经设置了小部件,就像我见过其他人一样,但由于某种原因,它继续只显示默认的基于文本的日期字段输入。在此先感谢您的帮助!

Forms.py

from django import forms
from django.forms import ModelForm, DateInput, DateField, extras
from django.forms.extras.widgets import SelectDateWidget

class EmployeeForm(ModelForm):
    hire_date = forms.DateField(widget=extras.SelectDateWidget)

    class Meta:
        model = Employee

Models.py

from django.db import models

class Employee(models.Model):
    name = models.CharField(max_length=40)
    department = models.CharField(max_length=40)
    hire_date = models.DateField()

模板(create.html)

{% extends "base.html" %}
{% load sekizai_tags formset_tags %}
{% block base_content %}

{{ formset.media }}

<div id="main-wrapper">
    <div class="strongborder">
        <div id="main" class="container boldtext">
            <form id="myForm" method="post" class="12u">
                {% csrf_token %}
                {% for field in form %}
                    <div class="row">
                        <div class="2u">
                            {{ field.label_tag }}:
                        </div>
                        <div class="10u">
                            {{ field }}
                            {{ field.errors }}
                        </div>
                    </div>
                {% endfor %}

            <div class="row 12u">
                <input type="submit" value="Save New Employee">    
            </div>

            </form>
        </div>
    </div>
</div>


{% endblock %}

2 个答案:

答案 0 :(得分:0)

来自django进口表格
来自django.forms.extras.widgets导入SelectDateWidget

如果您使用的是django 1.7试试这个:

hire_date = forms.DateField(widget = SelectDateWidget())

您可以找到参考here

答案 1 :(得分:0)

我建议您不要修改字段,而是更改窗口小部件。它适用于1.6。

from django.forms.extras.widgets import SelectDateWidget
from django.forms import ModelForm
from models import Employee

class EmployeeForm(ModelForm):
    class Meta:
        model = Employee
        widgets = {'hire_date': SelectDateWidget()}}

请注意,应该传递一个实例而不是一个类。