Django,如何在没有 for 循环的情况下解析单个表单字段(HTML)
我想单独解析模型表单的字段,而不是在 for 循环中。我想要一些字段来解析该元素的 Django HTML。 我有这个:
<form action="#" method="POST" name="notifictaionForm">
<ul>
<li><label>{{ form.fields.title.label }}</label> <span>{{ notification.title }}</span></li>
<li><label>{{ form.fields.create_date.label }}</label> <span>{{ notification.create_date }}</span></li>
<li><label>{{ form.fields.description.label }}</label> <span>{{ notification.description }}</span></li>
<li><label>{{ form.fields.status.label }}</label> <span>{{ form.fields.status.??? }}</span></li>
</ul>
</form>
所以我可以解析字段的名称,但不能解析 HTML 元素,我想在模板代码中的 ???
处解析 Django HTML 表单元素。
有人知道该怎么做吗?
I want to parse the fields of my Modelform separately and not in a for loop. And i want some fields to parse the Django HTML for that element.
I have this:
<form action="#" method="POST" name="notifictaionForm">
<ul>
<li><label>{{ form.fields.title.label }}</label> <span>{{ notification.title }}</span></li>
<li><label>{{ form.fields.create_date.label }}</label> <span>{{ notification.create_date }}</span></li>
<li><label>{{ form.fields.description.label }}</label> <span>{{ notification.description }}</span></li>
<li><label>{{ form.fields.status.label }}</label> <span>{{ form.fields.status.??? }}</span></li>
</ul>
</form>
So i can parse the name of the field but not the HTML element, i want at the ???
in the template code to parse the Django HTML form element.
Does somebody know how to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我很好地理解了你的意思,你想要 django 自动生成
status
字段的 html 吗?那么就非常简单了:几句话:
Form 是一个类似字典的对象,可以像这样访问字段:
声明的字段存储在
form.fields
中,一个 SortedDict 对象。因此,您可以使用此变量来访问字段,但推荐的方法始终是最短的方法。如果您是 Python 新手,您可能想知道如何将字段声明为属性,但是您无法像这样从 python 代码访问它们:
这是因为 python 中的类不是静态的,元 -类可用于在类定义之外构建各种新事物。 Django 利用它来创建友好的 API。基本上是这样的:
django.forms.Form
继承的__metaclass__
属性,并将其设置为:DeclarativeFieldsMetaclass。.base_fields
属性。base_fields
而不是fields
?好吧,这是另一个故事,这与来自 ModelForm 中模型的字段如何与表单类中声明的字段分离有关。但不要将元类与
class Meta
混淆,后者有时用于为表单或模型提供额外的配置选项。现在回到模板。您无法从Python代码访问
form.field_name
,为什么可以在模板中访问?如 django 文档中所述,当模板系统遇到点时,它按以下顺序尝试以下查找:这意味着模板系统在遇到:
{{ form.field_name }}
时将尝试返回的第一件事是:表单['field_name']
。If I've understood you well, You want django's auto-generated html for
status
field? Then it is very simple:Few extra words:
Form is dict-like object where fields can be accessed like this:
Declared fields are stored in
form.fields
, a SortedDict object. So you can use this variable to access fields, however recommended way is always the shortest way.If you are new to Python, you might wonder how is it so that you declare fields as attributes, however you are not able to access them from python code like this:
Well it is because classes in python aren't static, the meta-class can be used to build all kind of new things out of class definition. Django makes use of it to create a friendly API. Basically it goes like this:
__metaclass__
attribute which is inherited fromdjango.forms.Form
and is set to: DeclarativeFieldsMetaclass..base_fields
attribute is created.base_fields
notfields
? Well it is another story, this has do with how fields which comes from models in ModelForm are separated from fields declared in form class.But do not confuse metaclasses with the
class Meta
which is sometimes used to provide additional configuration options to your form or model.Back to templates now. You can not access
form.field_name
from python code, why then it is possible in template? As described in django documentation, when the template system encounters a dot, it tries the following lookups, in this order:That means first thing template system will try to return when it encounters:
{{ form.field_name }}
is:form['field_name']
.