我目前正在檢查我的模板中的權限以決定是否要顯示特定的鏈接。在模板標籤中使用django權限
這都非常好,我的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%}' – slackmart
是的是在示例中忘了它,但我正確地使用它;模板標籤按預期工作,而不是其中的驗證部分 – Malcoolm