简体   繁体   English

Django Jinja 如何通过模板查看用户是否有权限?

[英]Django Jinja How to check whether the user has any rights through the template?

I have a menu whose parts are displayed depending on the rights.我有一个菜单,其部分根据权限显示。 I configured it.我配置了它。 I want to check if the user has any rights in principle, if they do not, then the menu will not reach the moment of drawing.我想检查用户原则上是否有任何权限,如果没有,则菜单不会到达绘制的那一刻。 Something like this:像这样的东西:

   {% if perms.registration2 == NUll %}
      # render something
   {% endif %}

This code returns set() not NULL or '' :此代码返回set()而不是NULL''

{{ perms.registration2 }}

How can I do such a check?我怎样才能做这样的检查?

You can check permission in your template like this:您可以像这样检查模板中的权限:

{% if perms.app_label.permission_name %}
   # do something 
{% endif %}

If your trying to check whether the current user has any permission or not you can try this:如果您尝试检查当前用户是否有任何权限,您可以尝试以下操作:

 {% if not request.user.permissions.all %}
            # do something
   {% endif %}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Jinja模板(Django Framework)中检查数字是否可被另一个数整除 - How to check whether a number is divisible by another in Jinja Template (Django Framework) 如何在django模板中检查用户是否在线? - How to check whether a user is online in django template? 如何检查jinja2/django模板中任何变量的大小写? - How to check the case of any variable in jinja2/django template? 如何检查用户是否已经通过模板或视图评分 function django - How to check if the user already rated through template or view function django 如果用户具有删除权限,如何签入 Django 模板? - How to check in Django template if user has delete permission? 通过模板标签Django检查用户是否喜欢该帖子 - Checking whether user did like the post or not through template tag Django 如何检查用户是否在Django中成功创建 - How to check whether a user created successfully in Django 如何检查用户是否在 Django 中登录? - how to check whether a user is logged in in django? 如何检查列表的任何元素在Django的arrayfield中是否存在? - How to check whether any elements of a list exist in an arrayfield in django? 在 Django 模板中:检查用户是否有权访问按名称指定的视图 - In Django template: Check if user has permission to access view specified by name
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM