我目前正试图将SelectDateWidget应用于Django中的所有DateFields。我已经尝试了 here 和 here 的设置。尽管尝试了这些设置,表单仍然显示为默认的日期字段文本输入。非常感谢任何和所有的帮助!
from django import forms
from django.forms import ModelForm, DateInput, DateField
from django.forms.extras.widgets import SelectDateWidget
from extra_views import InlineFormSet, CreateWithInlinesView, UpdateWithInlinesView
from extra_views.generic import GenericInlineFormSet
from django.forms.models import inlineformset_factory
from employee_summary.models import Employee, Work_Record, Training_Record, FAA_Certificate
class EmployeeForm(ModelForm):
class Meta:
model = Employee
#This method doesn't seem to work
red_badge = forms.DateField(widget=extras.SelectDateWidget)
#This method doesn't seem to work either
def __init__(self, *args, **kwargs):
super(EmployeeForm, self).__init__(*args, **kwargs)
this_year = datetime.date.today().year
years = range(this_year-100, this_year+1)
years.reverse()
self.fields["hire_date"].widget = SelectDateWidget(years=years)
class WorkRecordFormSet(InlineFormSet):
model = Work_Record
prefix = "work_record"
extra = 1
class TrainingRecordFormSet(InlineFormSet):
model = Training_Record
prefix = "training_record"
extra = 1
class FAACertificateFormSet(InlineFormSet):
model = FAA_Certificate
prefix = "faa_certificate"
extra = 1
{% extends "base.html" %}
{% load sekizai_tags formset_tags %}
{% block base_content %}
<link rel="stylesheet" href="/media/themes/txt/css/employee_summary/report.css" />
{{ 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 %}
{% for formset in inlines %}
<div id="formset_{{ forloop.counter0 }}" data-formset-prefix="{{ formset.prefix }}">
{{ formset.management_form }}
<!-- New forms will be inserted in here -->
<div data-formset-body>
{% for form in formset %}
<br>
<br>
<h1>{{ form.instance.form_name }}</h1>
<div data-formset-form>
{% for field in form %}
<div class="row">
{% if field.label != "Employee" and field.label != "Id" and field.label != "Delete" %}
<label class="2u">{{ field.label }}:</label>
<div class="10u">{{ field }}</div>
{{ field.errors }}
{% endif %}
</div>
{% endfor %}
</div>
{% endfor %}
</div>
<!-- The empty form template. By wrapping this in a <script> tag, the
__prefix__ placeholder can easily be replaced in both attributes and
any scripts -->
<script type="form-template" data-formset-empty-form>
{% escapescript %}
<br>
<br>
<div data-formset-form>
{% for field in formset.empty_form %}
<div class="row">
{% if field.label != "Employee" and field.label != "Id" and field.label != "Delete" %}
<label class="2u">{{ field.label }}:</label>
<div class="10u">{{ field }} <br> {{ field.errors }}</div>
{% endif %}
</div>
{% endfor %}
</div>
{% endescapescript %}
</script>
<!-- This button will add a new form when clicked -->
<button type="button" class="text-green" data-formset-add><i class="fa fa-plus-circle"></i> Add another</button>
<script>jQuery(function($) {
$("#formset_{{ forloop.counter0 }}").formset({
animateForms: true
});
});</script>
</div>
{% endfor %}
<br>
<br>
<div class="row 12u">
<input type="submit" value="Save New Employee">
</div>
</form>
</div>
</div>
</div>
{% endblock %}
答案 0 :(得分:0)
我使用SelectDateWidget
以两种形式使用了两个字段;
import datetime
from django import forms
from django.forms.extras.widgets import SelectDateWidget
from consoles.models import Runner
from generic.forms.widgets import SelectTimeWidget
class RunnerDetailsForm(forms.ModelForm):
best_time = forms.TimeField(
widget=SelectTimeWidget(dashed=True, race_time=True)
)
years_to_display = range(datetime.datetime.now().year - 100,
datetime.datetime.now().year)
dob = forms.DateField(
widget=SelectDateWidget(years=years_to_display)
)
predicted_time = forms.TimeField(
widget=SelectTimeWidget(dashed=True, race_time=True)
)
class Meta:
model = Runner
只需在表单中定义字段,然后将SelectDateWidget
添加到每个字段:)
答案 1 :(得分:0)
您可以在widgets
中使用ModelForm
字典。相关的Django文档是here,他们说:
要为字段指定自定义窗口小部件,请使用内部Meta类的widgets属性。这应该是将字段名称映射到窗口小部件类或实例的字典。
对于您而言,这意味着:
from django.forms import widgets
class EmployeeForm(ModelForm):
class Meta:
model = Employee
fields = ['red_badge']
widgets = {'red_badge': widgets.SelectDateWidget()}