我有一个模板,它呈现通过一个视图处理的两种表单。第一个表单“store_form”是一个引导模式,在大多数情况下,用户不需要打开它并向它提交数据。第二个表单“wo_form”将与 store_form 分开提交。当我提交到 store_form 时,内联 is_valid() 验证错误消息会正确显示。如果未正确填写字段,如何让 wo_form 显示验证错误消息?
此外,如果用户提交到 store_form,是否有办法重新输入先前放入 wo_form 的数据?它当前执行重定向,并且已放入 wo_form 的数据丢失。
视图.py:
def work_order_form(request):
if request.method == 'POST':
if 'store_form' in request.POST:
store_form = StoreForm(request.POST)
wo_form = WorkOrderForm()
if store_form.is_valid():
store_form.save()
else:
store_form = StoreForm()
wo_form = WorkOrderForm(request.POST)
if wo_form.is_valid():
wo_form.save()
else:
wo_form = WorkOrderForm()
store_form = StoreForm()
return render_to_response('work_order_form.html', RequestContext(request, {
'wo_form': wo_form,
'store_form': store_form,
}))
我的 forms.py 非常基础:
class WorkOrderForm(forms.ModelForm):
class Meta:
model = WorkOrder
labels = {
'name': 'Work Order',
'nte': 'NTE',
}
widgets = {
'date_received': BootstrapDateInput(),
'trip_date': BootstrapDateInput(),
}
class StoreForm(forms.ModelForm):
class Meta:
model = Store
work_order_form.html(由于我想避免默认表单显示而感到困惑):
<div class="row">
<form class="form-{{ layout }}" action="" method="post" name="form-type" value="wo_form">
<div class="span4 offset1">
{% csrf_token %}
{{ wo_form.name.label_tag }}
{{ wo_form.name }}
<div class="row">
<div class="span1">
<label for="wo_form.complete.label" class="checkbox">
<input type="checkbox"> {{ wo_form.complete.label }}
</label>
</div>
<div class="span1">
<label for="wo_form.emergency.label" class="checkbox">
<input type="checkbox"> {{ wo_form.emergency.label }}
</label>
</div>
<div class="span2">
<label for="wo_form.after_hours.label" class="checkbox">
<input type="checkbox"> {{ wo_form.after_hours.label }}
</label>
</div>
</div>
{{ wo_form.date_received.label_tag }}
{{ wo_form.date_received }}
{{ wo_form.trip_date.label_tag }}
{{ wo_form.trip_date }}
{{ wo_form.time_in.label_tag }}
{{ wo_form.time_in }}
{{ wo_form.time_out.label_tag }}
{{ wo_form.time_out }}
{{ wo_form.total_hours.label_tag }}
{{ wo_form.total_hours }}
{{ wo_form.nte.label_tag }}
{{ wo_form.nte }}
{{ wo_form.instructions.label_tag }}
{{ wo_form.instructions }}
<label for="wo_form.completed_on_trip.label" class="checkbox">
<input type="checkbox"> {{ wo_form.completed_on_trip.label }}
</label>
<input type="submit" name="wo_form" value="Submit" class="btn btn-primary">
</div>
<div class="span4">
{{ wo_form.location.label_tag }}
{{ wo_form.location }}
<br>
</form>
<a data-toggle="modal" class="contact" href="#storeModal" title="Edit">New Store</a>
<div class="modal hide" id="storeModal">
<form class="well contact-form" method="post" action="" name="form-type" value="store_form">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h3>Editing Store</h3>
</div>
<div class="modal-body">
{% csrf_token %}
{{ store_form|as_bootstrap }}
</div>
<div class="modal-footer">
<input name="store_form" class="btn btn-primary" type="submit" value="Save" />
<input name="cancel" class="btn" type="submit" value="Cancel"/>
</div>
</form>
</div>
</div>
</div>
任何帮助将不胜感激。让我知道是否有任何其他资源会有所帮助!