python - Django Formset:对象没有属性'get'
问题描述
我想我快到了,但最后一部分只是不想工作:(。我希望你能提供帮助,因为两天后我再也看不到它了。
在我的 FormWizard 中,我试图(1)显示基于前一组中的滑块输入的 Formset(设置额外值)和(2)在 Formset 中的每个表单中我想显示一个 ChoiceField(forms.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 函数不正确吗?非常感谢您查看我的问题:)
解决方案
表单向导中的方法get_form_initial
应该返回一个字典,其中包含将要创建的表单的初始数据,而不是表单本身。如果要修改整个表单,请尝试修改get_form
方法。
推荐阅读
- bash - 如何逃避被单引号包围的回击?
- haskell - 导入 Control.Monad.Par 不工作
- javascript - D3.js v4:放大气泡图
- javascript - 如何在解析 HTML 和 JavaScript 后执行 JavaScript,但不执行任何其他资源(图像、样式表等)?
- sql - BigQuery 中的多个左连接
- bash - 如何 sudo bash 关键字,如 [[
- r - 当 ggplot() 为空且存在多个阴影 geom_rect 时基于 geom_point 的分面
- java - 使用 iText 2.1.7 合并大型 PDF
- amazon-web-services - AWS 与 GCP 成本模型
- python-3.x - 列表中的变量 - 通用用户界面,sys.argv