Django's documentation says that by creating the following form:
Django的文档说通过创建以下形式:
from django import forms
class NameForm(forms.Form):
your_name = forms.CharField(label='Your name', max_length=100)
the following form will be rendered:
将呈现以下表格:
<label for="your_name">Your name: </label>
<input id="your_name" type="text" name="your_name" maxlength="100">
Yet for some reason when I create an instance of this form and print it in my terminal I get the following.
然而出于某种原因,当我创建这个表单的实例并将其打印在我的终端中时,我得到以下内容。
NameForm()
print(NameForm())
<tr><th><label for="id_your_name">Your name:</label></th><td><input id="id_your_name" maxlength="100" name="your_name" type="text" /></td></tr>
The weirdest part of this is that when I send this form to my template via a context dictionary I get:
最奇怪的是,当我通过上下文字典将此表单发送到我的模板时,我得到:
<label for="your_name">Your name: </label>
<input id="your_name" type="text" name="your_name" maxlength="100">
So even though I do get what documentation states I should get, why does it render as a table prior to hitting the browser i.e. render as
所以即使我确实得到了我应该得到的文档状态,为什么它在点击浏览器之前呈现为表格,即呈现为
<tr><th><label for="id_your_name">Your name:</label></th><td><input id="id_your_name" maxlength="100" name="your_name" type="text" /></td></tr>
in my terminal?
在我的航站楼?
2 个解决方案
#1
1
You can see here Outputting forms as HTML that when you directly print a form instance the default output is a <table>
您可以在此处看到将表单输出为HTML,当您直接打印表单实例时,默认输出为
But when you render the form inside a template using {{ form }}
, then these table tags are omitted.
但是当您使用{{form}}在模板中呈现表单时,将省略这些表标记。
#2
0
As mentioned by Kartik Anand, this is just default templating of django form and there are more of them.
正如Kartik Anand所提到的,这只是django形式的默认模板,而且还有更多。
But if you're not satisfied with default, there is way to print it in your template as you like. Django form is an iterable and when iterating through, it will give you each field from form one by one. Each field will be of class BoundField
that will allow you to get full tags for label, field and errors list.
但是如果您对默认值不满意,可以根据需要在模板中打印它。 Django表单是一个可迭代的,当迭代时,它将逐个从表单中为您提供每个字段。每个字段都是BoundField类,它允许您获取标签,字段和错误列表的完整标签。
So if you want to build plain form, you can do:
因此,如果您想构建普通表单,您可以:
for bound_field in form:
print(bound_field.label_tag())
print(bound_field)
print(bound_field.errors)
#1
1
You can see here Outputting forms as HTML that when you directly print a form instance the default output is a <table>
您可以在此处看到将表单输出为HTML,当您直接打印表单实例时,默认输出为
But when you render the form inside a template using {{ form }}
, then these table tags are omitted.
但是当您使用{{form}}在模板中呈现表单时,将省略这些表标记。
#2
0
As mentioned by Kartik Anand, this is just default templating of django form and there are more of them.
正如Kartik Anand所提到的,这只是django形式的默认模板,而且还有更多。
But if you're not satisfied with default, there is way to print it in your template as you like. Django form is an iterable and when iterating through, it will give you each field from form one by one. Each field will be of class BoundField
that will allow you to get full tags for label, field and errors list.
但是如果您对默认值不满意,可以根据需要在模板中打印它。 Django表单是一个可迭代的,当迭代时,它将逐个从表单中为您提供每个字段。每个字段都是BoundField类,它允许您获取标签,字段和错误列表的完整标签。
So if you want to build plain form, you can do:
因此,如果您想构建普通表单,您可以:
for bound_field in form:
print(bound_field.label_tag())
print(bound_field)
print(bound_field.errors)