13

我有如下表格:

class MyForm(Form):

  #personal data
  firstname = CharField()
  lastname = CharField()

  #education data
  university = CharField()
  major = CharField()

  #foobar data
  foobar = ChoiceField()

由于某些字段(如 foobar)是从数据库中填充的,我不能使用其他方法,只能让 Django 使用 form.as_ul 为我呈现它

我也希望我不必为了便于维护而将表格拆分为多种形式

有没有办法告诉 Django 在这些表单部分之间显示帮助文本,以便我可以输入一些关于如何填写表单的说明?

我希望表单呈现如下内容:

<form>

  <p>Here you enter your personal data...</p>
  <input name='firstname'>
  <input name='lastname'>

  <p>Here you enter your education data...</p>
  <input name='university'>
  <input name='major'>

</form>

我需要创建自己的小部件才能显示这些<P>标签,还是有更简单的方法?

谢谢

4

8 回答 8

15

不使用 form.as_ul 在模板中显示表单的一种方法是使用 django-uni-form。首先,您必须在此处下载并安装它。然后设置表单的代码可能如下所示:

from uni_form.helpers import FormHelper, Submit, Layout, Fieldset

class MyForm(Form):

    #personal data
    firstname = CharField()
    lastname = CharField()

    #education data
    university = CharField()
    major = CharField()

    #foobar data
    foobar = ChoiceField()

    # now attach a uni_form helper to display the form
    helper = FormHelper()

    # create the layout
    layout = Layout(
         # first fieldset
         Fieldset("Here you enter your personal data...",
             'firstname', 'lastname'),
         Fieldset("Here you enter your education data...",
             'university', 'major'),
         Fieldset('foobar')

    # and add a submit button
    sumbit = Submit('add', 'Submit information')
    helper.add_input(submit)

现在,要在模板中显示它,您可以这样做:

{% load uni_form %}
{% with form.helper as helper %}
    {% uni_form form helper %}
{% endwith %}

这将输出 HTML(大致)如下:

<form>
<fieldset><legend>Here you enter your personal data...</legend>
<input name='firstname'>
<input name='lastname'>
</fieldset>

<fieldset><legend>Here you enter your education data...</legend>
<input name='university'>
<input name='major'>
</fieldset>

<fieldset>
<input name='foobar'>
</fieldset>

</form>

有关 uni_form 的更多信息,请阅读他们的文档(请参阅上面的链接)。

PS:我意识到这个回复很晚,我相信你已经解决了这个问题,但我认为这对现在刚刚遇到这个问题的任何人都有帮助。

于 2009-11-10T02:07:41.813 回答
11

如果你想自定义表单,你不必渲染它form.as_ul。如果您正确设置了表单模型,Django 知道如何呈现 foobar ......试试吧......不用担心。

看看服务器上的 python 发送了你的页面。例如,如果它发送了一个这样的 django 表单:

return respond(request, user, 'templateName', { 'myform':myform })

那么templateName.html将有:

<blockquote>
<form>

    <p>Here you enter your personal data...</p>
    {{myform.firstname }}

    <p>Here you enter your education data...</p>
    {{myform.university }}

    <p> a choice field </p>
    {{myform.foobar}}

</form>
</blockquote>
于 2009-05-14T12:15:26.697 回答
4

Since each section is a collection of multiple, independent form fields, I recommend using a custom form template. This gives you absolute full control over the layout with minimal extra work. Django's Customizing the Form Template docs have the details.

于 2009-04-07T23:19:00.773 回答
2

还要记住,Django Form 对象只是字段的集合;HTML 表单标签和 Django 表单对象之间不需要 1:1 的对应关系。如果表单的各个部分实际上在逻辑上是分开的,您可以考虑将其拆分为三个表单,然后您可以在模板中呈现您想要的任何 HTML(但仍然在单个 HTML 表单标签内)。

当然,这是否是一个明智的解决方案在很大程度上取决于您的应用程序的设计和视图。

于 2009-04-08T10:29:34.357 回答
1

您知道的表单中有一个help_text字段。

forms.py中:

  • myField = forms.myFieldType(help_text="Helping friendly text to put in your form", otherStuff=otherStuff)

forms.html中:

  • {{form.myField.help_text}}
于 2010-01-11T17:55:35.403 回答
0

Even though your form is populated from a database, you should still be able to manually write out the form, or use a template. Check out the Django form documentation for more details.

于 2009-04-07T23:19:03.627 回答
0

对于那些与作者类似情况的人,我建议在表单的元素上使用 CSS:before和/或:after伪选择器。它们同样有效,并且可以让您的生活更轻松,因为您仍然可以使用.inputlabel{{ form.as_p }}

于 2010-07-27T12:41:15.117 回答
-1

同意@mipadi,似乎这是最简单的方法。

所以像

$('.selector').append('<html></html>')
于 2015-05-24T13:59:26.997 回答