Han*_*Sun 5 python django django-templates
例如,嵌套循环中有一个对象:
{% for fieldset in inline_admin_form %}
{% for line in fieldset %}
{% for field in line %}
{% if field.is_hidden %} {{ field.field }} {% endif %}
{% endfor %}
{% endfor %}
{% endfor %}
{% endif %}
Run Code Online (Sandbox Code Playgroud)
现在我想查看类名和一些有关的信息field.field,所以我field.field.__repr__()用来替换field.field.
然而,django模板在更改后抱怨它:
Variables and attributes may not begin with underscores: 'field.field.__repr__'
Run Code Online (Sandbox Code Playgroud)
有没有人对此有所了解?有没有更好的方法来调试django模板中的变量?(我试过{% debug %}但是当我想在嵌套循环中检查一个变量时发现它很糟糕..)
Jam*_*ate 13
{{ value|stringformat:'r' }}
Run Code Online (Sandbox Code Playgroud)
使用字符串%运算符样式格式与使用的r格式repr()
您可以轻松编写一个模板过滤器,它允许您执行以下操作{{ var|asrepr }}。查看文档,但它看起来像这样:
@register.filter
def asrepr(value):
return repr(value)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1552 次 |
| 最近记录: |