如何在模板中显示ChoiceField?
摘录自
forms.py:views.py:index.html
class ContForm():
KafChoices = [(kaf.id, kaf.name) for kaf in Kaf.objects.all()]
kaf = forms.ChoiceField(choices = KafChoices, required = True)
class Meta:
model = Cont
def index(request):
return render_to_response('db3/index.html',
{'form': ContForm()},
context_instance=RequestContext(request))
:
{{ form.kaf }}
我得到以下输出: django.forms.fields.ChoiceField 对象位于 0x01A110D0
而不是通常的领域。怎么了?
Excerpts from
forms.py:
class ContForm():
KafChoices = [(kaf.id, kaf.name) for kaf in Kaf.objects.all()]
kaf = forms.ChoiceField(choices = KafChoices, required = True)
class Meta:
model = Cont
views.py:
def index(request):
return render_to_response('db3/index.html',
{'form': ContForm()},
context_instance=RequestContext(request))
index.html:
{{ form.kaf }}
I get following output:
django.forms.fields.ChoiceField object at 0x01A110D0
instead of usual field. What's wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因为
ContForm
不是继承自forms.Form
吗?编辑:看起来像一个模型表单,在这种情况下
ContForm(forms.ModelForm)
,但无论哪种方式,如果您不从表单类继承,所有form.kaf
都会做正在拉起这个 python 对象,它不知道如何处理自己。Because
ContForm
isn't inheriting fromforms.Form
?Edit: looks like a modelform, in which case
ContForm(forms.ModelForm)
, but either way if you don't inherit from the form classes allform.kaf
is doing is pulling up this python object that doesn't know what to do with itself.