我正在尝试创建一个FormWizard,以便一次添加模型的多个实例。第二页是动态的,基于要添加的数字,并创建一个“序列号”字段 - 所有实例之间的区别因素。
我正在向导的第一页很好,但第二页 - 动态的 - 正在给我这个验证错误:
[u'ManagementForm数据丢失或被篡改。']
我所做的所有搜索都指向了一个我没有使用的formset问题。我做错了什么?
forms.py
class CarrierWizardForm1(forms.Form):
part_numbers = forms.ModelChoiceField(queryset=PartNumber.objects.all())
expiry_date = forms.DateField()
qty_at_new = forms.IntegerField()
unit_cost = forms.DecimalField(max_digits=10, min_value=0, decimal_places=3)
cost_currency = forms.ChoiceField(choices=CURRENCY_CHOICES)
class CarrierWizardForm2(forms.Form):
def __init__(self, *args, **kwargs):
qty = kwargs.pop('qty')
super(CarrierWizardForm2,self).__init__(*args,**kwargs)
for i in qty:
self.fields['serial_%s' % i] = forms.CharField(max_length=45)
CarrierWizardForm2中的defs是一个相当常见的习惯用法,在整个网络上都被注意为这个问题的解决方案,包括Jacobian也不是他们在网址中疯狂的东西
urls.py
carrier_wizard_forms = [CarrierWizardForm1, CarrierWizardForm2]
...
url(r'^carrier/add/$', views.CarrierCreateWizard.as_view(carrier_wizard_forms)),
我的观点相对复杂,但没有什么可以令人发指的。请注意,有些字段不在表单中 - 它们填充了上下文数据(用户,创建日期等)
views.py
TEMPLATES = {"0": "inventory/carrier_wizard_form1.html",
"1": "inventory/carrier_wizard_form2.html"}
class CarrierCreateWizard(SessionWizardView):
def get_template_names(self):
return [TEMPLATES[self.steps.current]]
''' Get the qty from form 1 to indicate how many fields
form2 needs for serial numbers
'''
def get_form_initial(self, step):
current_step = self.storage.current_step
if current_step == 'step1':
prev_data = self.storage.get_step_data('step0')
return self.initial_dict.get(step, {'qty': qty})
return self.initial_dict.get(step, {})
def done(self, form_list, **kwargs):
details = form_list[0]
list_of_serial_nos = form_list[1]
for serial_name, serial in list_of_serial_nos.cleaned_data.items():
carrier = Carrier()
carrier.part_numbers = details.cleaned_data['part_numbers']
carrier.creation_date = datetime.datetime.today()
carrier.expiry_date = details.cleaned_data['expiry_date']
carrier.qty_at_new = 1
carrier.qty_current = 1
carrier.serial = serial
carrier.unit_cost = details.cleaned_data['unit_cost']
carrier.cost_currency = details.cleaned_data['cost_currency']
carrier.user = self.request.user
carrier.save()
我的第二个模板很平淡,尽管您可以看到三次尝试纠正此问题的尝试失败。
广告/ carrier_wizard_form2.html
{% extends "base.html" %}
{% block content %}
<p>Step {{ wizard.steps.step1 }} of {{ wizard.steps.count }}</p>
<form action="" method="post">{% csrf_token %}
{# Attempt 3 #}
{{ formset }}
{# Attempt 2 {{ form.management_form }}
{% for f in form %}
{{ f.as_p }}
{% endfor %}
#}
{# Attempt 1 {{ form }} #}
<input type=submit>
</form>
{% endblock %}
修改 * 根据要求,我的两个模板 *
库存/ carrier_wizard_form1.html
{% extends "base.html" %}
{% block content %}
<p>Step {{ wizard.steps.step1 }} of {{ wizard.steps.count }}</p>
<form action="" method="post">{% csrf_token %}
{{ wizard.form.as_p }}
<input type=submit>
</form>
{% endblock %}
模板/库存/ carrier_wizard_form2.html
{% extends "base.html" %}
{% block content %}
<p>Step {{ wizard.steps.step1 }} of {{ wizard.steps.count }}</p>
<form action="" method="post">{% csrf_token %}
<table>
{{ wizard.form }}
{#
{{ wizard.management_form }}
{% if wizard.form.forms %}..
{{ wizard.form.management_form }}
{% for form in wizard.form.forms %}
{{ form }}
{% endfor %}
{% else %}
{{ wizard.form }}.
{% endif %} #}
</table>
<input type=submit>
</form>
{% endblock %}
答案 0 :(得分:1)
您的模板不正确,请使用{{ wizard.management_form }}
添加与向导管理相关的内容,并使用{{ wizard.form }}
表单。
从reference doc模板就像:
{% extends "base.html" %}
{% block content %}
<p>Step {{ wizard.steps.step1 }} of {{ wizard.steps.count }}</p>
<form action="" method="post">{% csrf_token %}
{{ wizard.management_form }} {#add wizard management data #}
{% if wizard.form.forms %} {# if form is formset #}
{{ wizard.form.management_form }} {#formset's management data #}
{% for form in wizard.form.forms %}
{{ form }}
{% endfor %}
{% else %}
{{ wizard.form }} {#for normal form #}
{% endif %}
</table>
{% if wizard.steps.prev %}
<button name="wizard_goto_step" type="submit" value="{{ wizard.steps.first }}">{% trans "first step" %}</button>
<button name="wizard_goto_step" type="submit" value="{{ wizard.steps.prev }}">{% trans "prev step" %}</button>
{% endif %}
<input type="submit" value="{% trans "submit" %}"/>
</form>