【发布时间】:2017-09-10 02:37:38
【问题描述】:
我目前正在检查模板中的权限,以决定是否要显示特定链接。
这对于在我的普通模板中扩展的 base.html 非常有效:
base.html
{% if perms.myapp.can_add %}
#display link
{% endif %}
我的模板
{% extends "riskass/base.html" %}
{% block content %}
# do stuff #
{% endblock %}
但我也对模板中的重复项目使用模板标签,并且相同的权限检查似乎在它们中不起作用。
有人知道我可能做错了什么吗?谢谢!
我的模板
{% extends "riskass/base.html" %}
{% load show_items %}
{% block content %}
# do stuff #
{% show_items items_list%}
{% endblock %}
templatetags/show_items.py
from django import template
register = template.Library()
@register.inclusion_tag('myapp/show_items.html')
def show_items(items):
return {'items': items}
myapp/show_items.html
{% for item in items%}
# display stuff: this works
...
# check permissions:
{% if perms.myapp.can_add %}
#display other link: doesn't do anything
{% endif %}
【问题讨论】:
-
作为建议:使用
{% load show_item%}是不够的,还需要使用{% show_item AnObject %} -
是的,是的,在示例中忘记了它,但我正确地使用了它;模板标签按预期工作,而不是其中的 Auth 部分