python - Django:将 ChoiceField 选择作为位置参数传递给表单的 `__init__()` 会导致 AttributeError

标签 python django django-forms

我试图将参数从views.py传递到forms.py以创建带有CheckboxSelectMultiple小部件的ChoiceField,但渲染失败。

Exception Type: AttributeError Exception Value: 'list' object has no attribute 'get'.

您能看一下下面并指出我做错了什么吗:(

感谢您提前提供的帮助。

表单.py:

class TestSubmitForm(forms.Form):
    tests = forms.ChoiceField()

    def __init__(self,*args,**kwargs):
        self.testList = args[0]
        super(TestSubmitForm,self).__init__(*args,**kwargs)
        self.fields['tests'].widget = forms.CheckboxSelectMultiple()
        self.fields['tests'].choices = self.testList

View .py

def index(request):
    tc_obj_form = [("","1"),("","2"),("",3")]
    tests = TestSubmitForm(tc_obj_form)
    return render(request, 'index.html',{'tests':tests})

index.html

<form action="." method="POST">
    <table >
        {% csrf_token %}
        {{ tests }}
    </table>
    <input type="submit" value="Submit"/>
</form>

堆栈跟踪:

Traceback: File "/local/User/demo/web/venv/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  111.                     response = wrapped_callback(request, *callback_args, **callback_kwargs) File "/local/User/demo/web/apps/Lcloud/views.py" in index
  94.           return render(request, 'Lcloud/index.html',
  ...
  135.         return self.as_table() File "/local/User/demo/web/venv/lib/python2.7/site-packages/django/forms/forms.py" in as_table
  257.             errors_on_separate_row=False) File "/local/User/demo/web/venv/lib/python2.7/site-packages/django/forms/forms.py" in _html_output
  181.         top_errors = self.non_field_errors()  # Errors that should be displayed above all fields. File "/local/User/demo/web/venv/lib/python2.7/site-packages/django/forms/forms.py" in non_field_errors
  283.         return self.errors.get(NON_FIELD_ERRORS, self.error_class()) File "/local/User/demo/web/venv/lib/python2.7/site-packages/django/forms/forms.py" in errors
  154.             self.full_clean() File "/local/User/demo/web/venv/lib/python2.7/site-packages/django/forms/forms.py" in full_clean
  353.         self._clean_fields() File "/local/User/demo/web/venv/lib/python2.7/site-packages/django/forms/forms.py" in _clean_fields
  362.             value = field.widget.value_from_datadict(self.data, self.files, self.add_prefix(name)) File "/local/User/demo/web/venv/lib/python2.7/site-packages/django/forms/widgets.py" in value_from_datadict
  585.         return data.get(name, None)

Exception Type: AttributeError at /Lcloud/ Exception Value: 'list' object has no attribute 'get'

最佳答案

按如下方式更改表单的 __init__():

def __init__(self, testList, args,**kwargs):
    self.testList = testList
    super(TestSubmitForm,self).__init__(*args,**kwargs)  # testList not in args!
    # ...

问题是 forms.Form__init__() (您通过 super(...) 调用)需要一个类 dict 的对象作为其第一个位置参数。通常,如果提供的话,它将是一个 QueryDict 实例,例如 request.POST 或某物。类似。

关于python - Django:将 ChoiceField 选择作为位置参数传递给表单的 `__init__()` 会导致 AttributeError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41665978/

相关文章:

python - 无法从源代码 : recipe for target 'Programs/python.o' failed 安装 python 3.6.3

django - 如何获取模板中当前应用程序的名称?

python - 在用户发送的 Django 管理页面中显示数据

ajax - 从 django ajax 函数返回多个变量

python - 选择特定用户

python - 如何在 Django 模板中循环访问表单字段的初始值

python - 如何将 Python 对象更改为 XML?

python - 仅使用圆形网格绘制三角形,python

python - 解压 PyInstaller 打包文件

python - 当已经托管 rstudio 和 rapache 时,将 django 站点添加到 ubuntu 上的 apache2