使用django-rules时,您可以在视图中检查权限。但是我的总是返回错误。在下面的示例中,代码总是显示" bar",但必须显示" foo":
{% extends 'home/base.html' %}
{% load rules %}
{% has_perm 'anApp.can_access_something' user as can_access_something %}
{% block content %}
{% if can_access_something %}
<p>foo</p>
{% else %}
<p>bar</p>
{% endif %}
...
{% endblock %}
我的规则。
@rules.predicate
def testrule(user):
return True
rules.add_perm('anApp.can_access_something', testrule)
因此,总是会返回true。
以下行必须在块中:
{% has_perm 'anApp.can_access_something' user as can_access_something %}
这将正确显示" foo":
{% extends 'home/base.html' %}
{% load rules %}
{% block content %}
{% has_perm 'anApp.can_access_something' user as can_access_something %}
{% if can_access_something %}
<p>something</p>
{% else %}
<p>something else</p>
{% endif %}
...
{% endblock %}