【发布时间】:2018-06-10 19:27:30
【问题描述】:
在这种情况下创建一个私有过滤器可以正常工作:
{{ value|private:'_id' }}
但在这种情况下它不起作用:
{% url 'value.show' value|private='_id' %}
有没有办法解决这个问题?
过滤器示例: Django Templates and MongoDB _id
我正在使用 couchdb 和 mongo 查询。
TemplateSyntaxError
Variables and attributes may not begin with underscores: 'value._id'
Request Method: GET
Exception Type: TemplateSyntaxError
Exception Value:
Variables and attributes may not begin with underscores: 'value._id'
Python Version: 3.6.4
【问题讨论】:
-
...不要使用下划线?
-
你能发布你得到的完整错误吗?你在哪里检索
_id,你可以将它设置为一个新的变量,比如id_to_display,这样模板就不会打架了吗? -
谢谢,但在这种情况下不是一个选项,因为我使用的是 couchdb
-
你试过了吗:
{% private='_id' as myNewID %}然后{% url 'value.show' myNewID %}? -
@register.simple_tag def underscoreTag(obj, attribute): obj = dict(obj) return obj.get(attribute) 感谢“Chiheb Nexus”,我也使用了你的提议。 {% underscoreTag value "_id" as id %}
标签: python django django-templates django-template-filters