简体   繁体   English

django form.as_p 中的表单不同

[英]Form in django form.as_p different

what form.to_p exacly return?什么 form.to_p 准确返回?

I mean我的意思是

this code:这段代码:

            <form action="{{ action }}" method="post" enctype="multipart/form-data">
                {% csrf_token %}
                        <input type="text" name="title_field" id="form.title}}"/>
                        <input type="text" name="author_field" id="form.author }}"/>
                        {{ form.content }}
                        <input type="submit" value="Send"/>
            </form>

doesn't work, instead of this code works:不起作用,而不是此代码有效:

            <form action="" method="post" enctype="multipart/form-data">
                {% csrf_token %}
                {{ form.as_p }}
                <input type="submit" value="Send"/>
            </form>

Of course in the first case I can stylize in html/css specific fields..当然,在第一种情况下,我可以在 html/css 特定字段中进行样式化。

@edit @编辑

by working I mean send forward.通过工作,我的意思是转发。 In first second doesn't do anything在第一秒什么都不做

Forms have a few optional rendering options in django: as_p, as_table, as_ul表单在 Django 中有几个可选的渲染选项:as_p、as_table、a​​s_ul

Without any rendering options:没有任何渲染选项:

<form action="" method="post" enctype="multipart/form-data">
{% csrf_token %}
{{ form }}
<input type="submit" value="Send"/>
</form>

Will render a form that looks like this:将呈现如下所示的表单:

<form action="" method="post" enctype="multipart/form-data">
<input>
<input>
<input>
...
</form>

Adding the rendering option as_p just wraps the input fields in paragraph tags.添加渲染选项 as_p 只是将输入字段包装在段落标签中。 So adding the as_p here:所以在这里添加 as_p :

<form action="" method="post" enctype="multipart/form-data">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Send"/>
</form>

Will render a form that looks like this:将呈现如下所示的表单:

<form action="" method="post" enctype="multipart/form-data">
<p><input></p>
<p><input></p>
<p><input></p>
...
</form>

To see what {{ form.as_p }} outputs, you can click 'view source' in your browser and see the rendered html.要查看{{ form.as_p }}输出的内容,您可以在浏览器中单击“查看源代码”并查看呈现的 html。

I would not recommend rendering the fields manually as in your first example.我不建议像您的第一个示例那样手动渲染字段。 It's easy to make mistakes.很容易犯错误。 For example you have forgotten the opening {{ in id="form.title}}" .例如,您忘记了开头{{ in id="form.title}}"

If you need to add a custom class to the input, you can do this by changing the field's widget .如果需要向输入添加自定义类,可以通过更改字段的小部件来实现 Alternatively, you might find crispy forms useful.或者,您可能会发现酥脆的形式很有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM