将标题插入Django表单 [英] Inserting A Heading Into A Django Form
问题描述
我希望我的表单输出如下信息:
I would like my form to output something like this:
Name: ___________
Company: ___________
Interested in
-------------
Foo: [ ]
Bar: [ ]
Baz: [ ]
也就是说,我想在我的中间插入有兴趣的标题表单输出。
That is to say, I would like to have the 'Interested In' title inserted in the middle of my form output.
这样做的一种方式是写出每个字段的模板代码,并根据需要插入标题。但是,我不想这样做。我怀疑与我想要的表单有关系。
One way of doing this would be to write out the template code for each field, and insert the heading as appropriate. However, I'd prefer not to do this. I suspect that there is something to do with formsets that will do what I want.
有没有办法做我想要的,或我只需要吸吮并接受我需要为此编写我的模板?
Is there a way to do what I want, or do I just need to suck it up and accept that I need to write my template out longhand for this?
推荐答案
使用 FieldsetMixin
你得到Admin-像fieldets。您创建的表单如下所示:
With FieldsetMixin
you get Admin-like fieldsets. You create your form like this:
from django.forms import Form
from formfieldset.forms import FieldsetMixin
class MyForm(Form, FieldsetMixin):
fieldsets = (
(u'', {'fields': ['name', 'company']}),
(u'Interested in', {'fields': ['foo', 'bar', 'baz']}),
)
# rest of the form
这篇关于将标题插入Django表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!