在Django表单字段之间显示一些自由文本 [英] Display some free text in between Django Form fields

查看:77
本文介绍了在Django表单字段之间显示一些自由文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个如下表格:

class MyForm(Form):

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

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

  #foobar data
  foobar = ChoiceField()

字段(如foobar)从数据库中填充,我不能使用另一种方法,除了让Django使用form.as_ul呈现给我。

Since some fields (like foobar) are populated from the database i can't use another method other than letting Django render it for me with form.as_ul

此外,我希望我不要有没有办法让Django在这些表单部分之间显示一个帮助文本,以便我可以把它放在多个表单之间,以便于维护

Also i wish i don't have to split the form in multiple forms for ease of mantainance

在一些关于如何填写表单的说明中?

Is there a way to tell Django to display a help text in between these form sections so that i can put in some instructions on how to fill the form?

我希望表单显示如下:

<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> 标签,还是有更简单的方法?

Would i need to create my own widget to be able to display those <P> tags, or is there an easier way?

谢谢

推荐答案

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

One way to do this without displaying your form in the template using form.as_ul, is with django-uni-form. First you'll have to download it here and install it. Then the code for setting up your form could looks something like this:

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)

现在,要在你的模板中显示这个这样做:

Now, to display this in your template you would do this:

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

这将输出HTML (大致)这样:

This would output HTML (roughly) like this:

<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的更多信息,请阅读他们的文档(请参阅上面的链接)。

For more info on uni_form, read their docs (see the link above).

PS:我意识到这个回复迟到了,我相信你已经解决了这个问题,但我认为这应该是有助于任何人刚刚遇到这个。

PS: I realize this reply is late, and I'm sure you already solved this problem, but I think this should be helpful for anyone just coming across this now.

这篇关于在Django表单字段之间显示一些自由文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆