Django 错误:“'ChoiceField' 对象没有属性 'is_hidden'”
Posted
技术标签:
【中文标题】Django 错误:“\'ChoiceField\' 对象没有属性 \'is_hidden\'”【英文标题】:Django error: "'ChoiceField' object has no attribute 'is_hidden'"Django 错误:“'ChoiceField' 对象没有属性 'is_hidden'” 【发布时间】:2014-08-31 23:37:11 【问题描述】:Django 模板在渲染时抛出“AttributeError”。我想要实现的是,在模板中,resolted forn 将包含一个选择框,其中包含下面列表中的值。
这是Forms.py
文件:
class CallForm (forms.ModelForm):
class Meta():
model = Call
widgets =
'employee_id' : forms.ChoiceField(choices=FormsTools.EmployeesToTuples(Employee.objects.all()))
解释:
FormsTools.EmployeesToTuples(Employee.objects.all()) --> [(1, u'E1'), (2, u'E2'), (3, u'E3')]
这是 django 的错误:
Template error:
In template C:\virtualenv\#\#\templates\new_call.html, error at line 7
'ChoiceField' object has no attribute 'is_hidden'
1 : % extends "base.html" %
2 :
3 : % block content %
4 :
5 : <form action="/NewCall/" method="post">% csrf_token %
6 : <ul>
7 : form.as_p
8 : </ul>
9 : <input type="submit" name="submit" value="Add New Call">
10 :
11 : </form>
12 :
13 : % endblock %
Traceback:
File "C:\Python27\lib\site-packages\django\core\handlers\base.py" in get_response
112. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\virtualenv\OmegaManager2\OmegaManager\ServiceManager\views.py" in new_call
32. html = t.render(Context(args))
File "C:\Python27\lib\site-packages\django\template\base.py" in render
140. return self._render(context)
File "C:\Python27\lib\site-packages\django\template\base.py" in _render
134. return self.nodelist.render(context)
File "C:\Python27\lib\site-packages\django\template\base.py" in render
840. bit = self.render_node(node, context)
File "C:\Python27\lib\site-packages\django\template\debug.py" in render_node
78. return node.render(context)
File "C:\Python27\lib\site-packages\django\template\loader_tags.py" in render
123. return compiled_parent._render(context)
File "C:\Python27\lib\site-packages\django\template\base.py" in _render
134. return self.nodelist.render(context)
File "C:\Python27\lib\site-packages\django\template\base.py" in render
840. bit = self.render_node(node, context)
File "C:\Python27\lib\site-packages\django\template\debug.py" in render_node
78. return node.render(context)
File "C:\Python27\lib\site-packages\django\template\loader_tags.py" in render
62. result = block.nodelist.render(context)
File "C:\Python27\lib\site-packages\django\template\base.py" in render
840. bit = self.render_node(node, context)
File "C:\Python27\lib\site-packages\django\template\debug.py" in render_node
78. return node.render(context)
File "C:\Python27\lib\site-packages\django\template\debug.py" in render
88. output = self.filter_expression.resolve(context)
File "C:\Python27\lib\site-packages\django\template\base.py" in resolve
585. obj = self.var.resolve(context)
File "C:\Python27\lib\site-packages\django\template\base.py" in resolve
735. value = self._resolve_lookup(context)
File "C:\Python27\lib\site-packages\django\template\base.py" in _resolve_lookup
789. current = current()
File "C:\Python27\lib\site-packages\django\forms\forms.py" in as_p
241. errors_on_separate_row = True)
File "C:\Python27\lib\site-packages\django\forms\forms.py" in _html_output
156. if bf.is_hidden:
File "C:\Python27\lib\site-packages\django\forms\forms.py" in is_hidden
560. return self.field.widget.is_hidden
Exception Type: AttributeError at /NewCall/
Exception Value: 'ChoiceField' object has no attribute 'is_hidden'
'AttributeError' 的原因是什么?如何修复? 谢谢。
【问题讨论】:
【参考方案1】:ChoiceField
不是小部件 - 它是 form field:
class CallForm (forms.ModelForm):
employee_id = forms.ChoiceField(choices=FormsTools.EmployeesToTuples(Employee.objects.all()))
class Meta:
model = Call
fields = ['employee_id']
【讨论】:
正确的做法是什么?如何将其传输到小部件? 你能简单解释一下小部件的用途是什么吗? @user2216190 很高兴它成功了。基本上小部件控制网页上字段的表示,小部件处理 HTML 表单的呈现,docs 对它们非常详细。以上是关于Django 错误:“'ChoiceField' 对象没有属性 'is_hidden'”的主要内容,如果未能解决你的问题,请参考以下文章
Django 表单 ChoiceField 依赖于另一个 ChoiceField
使用 ChoiceField 时,类型错误对象不是 JSON 可序列化的
如何在 Django 表单 ChoiceField 中获取选项标签?