【发布时间】:2019-07-21 06:52:09
【问题描述】:
我想我快到了,但最后一部分只是不想工作:(。我希望你能提供帮助,因为我在 2 天后再也看不到它了。
在我的 FormWizard 中,我试图 (1) 在前一组中显示基于滑块输入的表单集(设置额外值)和 (2) 在表单集中的每个表单中我想显示一个 ChoiceField(表单.Select) 基于上一步中的文本输入。
通过在 stackoverflow 上进行大量搜索,我能够执行第 1 步。第 2 步几乎可以工作,除了 ChoiceField 不会使用来自文本输入的新值进行更新。这是我在views.py 中的代码:
class FormWizardView(LoginRequiredMixin, SessionWizardView):
template_name = 'test/test.html'
def get_form_initial(self, step):
if step == 'formset_step':
form_class = self.form_list[step]
data = self.get_cleaned_data_for_step('slider_step')
if data is not None:
# To set the extra value in formset based on slider input
extra = data['number_slider']
form_class.extra = extra
# To set the ChoiceField value in formset based on text-input
form1_cleaned_data = self.get_cleaned_data_for_step('text_input_step')
formset = form_class().forms
for form in formset:
if form1_cleaned_data:
form.fields['text_input'].choices = [item for item in form1_cleaned_data.items()]
# Print form to test if the form updates
print(form)
return formset
return super(FormWizardView, self).get_form_initial(step)
def done(self, form_list, **kwargs):
do something
return something
我正在尝试返回表单集,但收到错误 'TestForm' object has no attribute 'get'。我可能在这里退回了错误的东西,但是无论我尝试返回什么,它都不起作用。返回super(FormWizardView, self).get_form_initial(step) 只会给我一个空的ChoiceField 并返回表单给我错误object of type 'TestForm' has no len()。
我还在控制台中打印了表单,这似乎可以正常工作。有谁知道我应该返回什么才能获得填充的 ChoiceField?
非常感谢!
编辑: 感谢您的回答!当我修改我的 get_form 时:
def get_form(self, step=None, data=None, files=None):
if step == 'formset_step':
form_class = self.form_list[step]
data = self.get_cleaned_data_for_step('slider_step')
if data is not None:
# To set the extra value in formset based on slider input
extra = data['number_slider']
form_class.extra = extra
# To set the ChoiceField value in formset based on text-input
form1_cleaned_data = self.get_cleaned_data_for_step('text_input_step')
formset = form_class().forms
for form in formset:
if form1_cleaned_data:
form.fields['text_input'].choices = [item for item in form1_cleaned_data.items()]
# Print form to test if the form updates
print(form)
return super(FormWizardView, self).get_form(step, data, files)
我收到错误 ['ManagementForm data is missing or has been tampered with']。通过 StackOverflow 浏览时,它似乎是一个模板问题(特别是没有设置 {{ wizard.management_form }},但我从 Django FormTools 文档中获取了应该正常工作的纯代码。在我的模板中我有这个:
{% extends "base.html" %}
{% load i18n %}
{% block head %}
{{ wizard.form.media }}
{% endblock %}
{% block content %}
<p>Step {{ wizard.steps.step1 }} of {{ wizard.steps.count }}</p>
<form action="" method="post">{% csrf_token %}
<table>
{{ wizard.management_form }}
{% if wizard.form.forms %}
{{ wizard.form.management_form }}
{% for form in wizard.form.forms %}
{{ form }}
{% endfor %}
{% else %}
{{ wizard.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>
{% endblock %}
我在模板中没有看到什么,还是我的 get_form 函数不正确?非常感谢您查看我的问题:)
【问题讨论】:
标签: python django django-forms formset django-formwizard