什么form.to_p exacly return?
我的意思是
此代码:
<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>
不起作用,而不是此代码有效:
<form action="" method="post" enctype="multipart/form-data">
{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Send"/>
</form>
当然,在第一种情况下,我可以在html / css特定字段中进行样式设计..
@edit
通过工作我的意思是发送。在第一秒没有做任何事情
答案 0 :(得分:1)
Forms have a few optional rendering options in django: as_p, as_table, as_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. So adding the as_p here:
<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>
答案 1 :(得分:0)
要查看{{ form.as_p }}
输出内容,您可以在浏览器中点击“查看来源”,然后查看呈现的html。
我不建议像第一个示例中那样手动渲染字段。犯错很容易。例如,您忘记了{{
中的开头id="form.title}}"
。
如果您需要在输入中添加自定义类,可以通过changing the field's widget执行此操作。或者,您可能会发现crispy forms有用。