python 在 Django 表单字段之间显示一些自由文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/727917/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 20:44:53  来源:igfitidea点击:

Display some free text in between Django Form fields

pythondjangodjango-forms

提问by Lorenzo

I have a form like the following:

我有一个如下所示的表格:

class MyForm(Form):

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

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

  #foobar data
  foobar = ChoiceField()

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

由于某些字段(例如 foobar)是从数据库中填充的,因此除了让 Django 使用 form.as_ul 为我呈现之外,我无法使用其他方法

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?

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

I'd like the form to render something like this:

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

<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>

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

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

Thanks

谢谢

回答by hora

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 hereand install it. Then the code for setting up your form could looks something like this:

不使用 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)

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

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

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

This would output HTML (roughly) like this:

这将输出 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>

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

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

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.

PS:我知道这个回复晚了,我相信你已经解决了这个问题,但我认为这对现在刚遇到这个问题的人应该有帮助。

回答by Lorenzo

If you want to customize the form, you don't have to render it form.as_ul. Django knows how to render foobar if you have set up the forms model properly...try it...no worries.

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

Look at what python on the server sent your page. For example if it sent a django form like this:

查看服务器上的python 发送了您的页面。例如,如果它发送了这样的 Django 表单:

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

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

then templateName.htmlwill have:

那么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>

回答by Jarret Hardie

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 Templatedocs have the details.

由于每个部分都是多个独立表单域的集合,我建议使用自定义表单模板。这使您能够以最少的额外工作完全控制布局。Django 的自定义表单模板文档有详细信息。

回答by Carl Meyer

Remember also that a Django Form object is just a collection of fields; there is no need for a 1:1 correspondence between HTML form tags and Django Form objects. If the various sections of the form are actually logically separate, you could consider splitting it up into three Forms, which you could then render in your template with any HTML you want between them (but still within a single HTML form tag).

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

Whether this is a sensible solution depends quite a bit on the design of your app and the view, of course.

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

回答by hendrixski

There is a help_textfield in forms you know.

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

in forms.py:

forms.py 中

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

in forms.html:

forms.html 中

  • {{form.myField.help_text}}
  • {{form.myField.help_text}}

回答by Nicolas R

For those into a similar situation as the author, I recommend falling back to CSS and the :beforeand/or :afterpseudoselectors either on the inputor the labelelements of the form. They work just as well and could make your life a lot easier as you can still use {{ form.as_p }}.

对于那些到了类似的情况作为作者,我建议回落至CSS和:before和/或:after无论是在pseudoselectorsinputlabel形式的元素。它们也能正常工作,并且可以让您的生活更轻松,因为您仍然可以使用{{ form.as_p }}.

回答by mipadi

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 documentationfor more details.

即使您的表单是从数据库填充的,您仍然应该能够手动写出表单,或使用模板。查看Django 表单文档以获取更多详细信息。

回答by mexekanez

agreed with @mipadi, seems it's the easiest way.

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

So something like

所以像

$('.selector').append('<html></html>')