如何在 Django 模板中查看上下文变量的所有可能属性
How can I see all possible attributes of an context variables in django templates
我正在尝试在一个视图中同时使用双模型形式,我正在使用 django-betterforms 合并它们,它将来自两个模型的所有字段合并为一个单一形式。我知道我可以使用不同的 class 和 id 来分隔它们,但是我不能以模板形式提取它们,比如
{{ form }}
它将在模板中放置完整的表单,我可以像这样渲染所有字段
{% for field in form %}
{{ field }} or anything
{% endfor %}
我的问题是我怎样才能知道这个字段的所有可能属性,比如
{{ field.* }} *=anything
有点dir(field)
.
这是我面临的一个问题,但是找到所有属性或在两侧分开两个表单的解决方案是什么。基本上我需要分开两个模型,它们会在同一时间保存相同的视图,但在前端它们会有所不同。
提前致谢!!!
您创建一个 custom filter:
在templatetags/my_filters.py:
from django import template
register = template.Library()
@register.filter
def getallattrs(value):
return dir(value)
在您的模板中:
{% load my_filters %}
...
{{ field|getallattrs }}
我正在尝试在一个视图中同时使用双模型形式,我正在使用 django-betterforms 合并它们,它将来自两个模型的所有字段合并为一个单一形式。我知道我可以使用不同的 class 和 id 来分隔它们,但是我不能以模板形式提取它们,比如
{{ form }}
它将在模板中放置完整的表单,我可以像这样渲染所有字段
{% for field in form %}
{{ field }} or anything
{% endfor %}
我的问题是我怎样才能知道这个字段的所有可能属性,比如
{{ field.* }} *=anything
有点dir(field)
.
这是我面临的一个问题,但是找到所有属性或在两侧分开两个表单的解决方案是什么。基本上我需要分开两个模型,它们会在同一时间保存相同的视图,但在前端它们会有所不同。
提前致谢!!!
您创建一个 custom filter:
在templatetags/my_filters.py:
from django import template
register = template.Library()
@register.filter
def getallattrs(value):
return dir(value)
在您的模板中:
{% load my_filters %}
...
{{ field|getallattrs }}