Django,如何在没有 for 循环的情况下解析单个表单字段(HTML)

发布于 2024-11-02 09:11:41 字数 878 浏览 4 评论 0原文

我想单独解析模型表单的字段,而不是在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼眸里的那抹悲凉 2024-11-09 09:11:42

如果我很好地理解了你的意思,你想要 django 自动生成 status 字段的 html 吗?那么就非常简单了:

{{ form.status }}

几句话:

Form 是一个类似字典的对象,可以像这样访问字段:

>>> form['field_name']

声明的字段存储在 form.fields 中,一个 SortedDict 对象。因此,您可以使用此变量来访问字段,但推荐的方法始终是最短的方法。

如果您是 Python 新手,您可能想知道如何将字段声明为属性,但是您无法像这样从 python 代码访问它们:

>>> form.field_name
AttributeError: 'Form' object has no attribute 'field_name'

这是因为 python 中的类不是静态的,元 -类可用于在类定义之外构建各种新事物。 Django 利用它来创建友好的 API。基本上是这样的:

  1. Python 解释器解析你的 Form 类。
  2. 解释器找到从django.forms.Form继承的__metaclass__属性,并将其设置为:DeclarativeFieldsMetaclass
  3. 元类重组你的类。属性被删除并创建 .base_fields 属性。
  4. 为什么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:

{{ form.status }}

Few extra words:

Form is dict-like object where fields can be accessed like this:

>>> form['field_name']

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:

>>> form.field_name
AttributeError: 'Form' object has no attribute 'field_name'

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:

  1. Python interpreter parses your Form class.
  2. Interpreter finds __metaclass__ attribute which is inherited from django.forms.Form and is set to: DeclarativeFieldsMetaclass.
  3. Metaclass restructures your class. Attributes are removed and .base_fields attribute is created.
  4. Why base_fields not fields? 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:

  • Dictionary lookup
  • Attribute lookup
  • Method call
  • List-index lookup

That means first thing template system will try to return when it encounters: {{ form.field_name }} is: form['field_name'].

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文