Python 如何检查(在模板中)用户是否属于一个组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34571880/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 15:11:06  来源:igfitidea点击:

How to check (in template) if user belongs to a group

pythondjangodjango-admindjango-permissions

提问by Milano

How to check in template whether user belongs to some group?

如何在模板中检查用户是否属于某个组?

It is possible in a viewwhich is generating the templatebut what if I want to check this in base.htmlwhich is an extending template (it does not have it's own view function)?

有可能在view它生成template但如果我想检查base.html它是一个扩展模板(它没有自己的视图函数)怎么办?

All of my templates extends base.htmlso it is not good to check it in each view.

我的所有模板都扩展了,base.html所以在每个view.

The base.htmlcontains upper bar, which should contain buttons depending on in which grouplogged user is (Customers, Sellers).

base.html包含上部杆,它应包含在其中的按钮根据group登录用户是(客户,销售商)。

In my base.htmlis:

在我的base.html是:

{% if user.is_authenticated %}

which is not enough because I have to act differently to users from Customersand users from Sellers.

这还不够,因为我必须对Customers来自Sellers.

So the thing I want is:

所以我想要的是:

{% if user.in_group('Customers') %}
 <p>Customer</p>
{% endif %}
{% if user.in_group('Sellers') %}
<p>Seller</p>
{% endif %}

采纳答案by mishbah

You need custom template tag:

您需要自定义模板标签:

from django import template

register = template.Library() 

@register.filter(name='has_group') 
def has_group(user, group_name):
    return user.groups.filter(name=group_name).exists() 

In your template:

在您的模板中:

{% if request.user|has_group:"mygroup" %} 
    <p>User belongs to my group 
{% else %}
    <p>User doesn't belong to mygroup</p>
{% endif %}

Source: http://www.abidibo.net/blog/2014/05/22/check-if-user-belongs-group-django-templates/

来源:http: //www.abidibo.net/blog/2014/05/22/check-if-user-belongs-group-django-templates/

Docs: https://docs.djangoproject.com/en/1.9/howto/custom-template-tags/

文档:https: //docs.djangoproject.com/en/1.9/howto/custom-template-tags/

回答by fuser60596

In your app create a folder 'templatetags'. In this folder create two files:

在您的应用程序中创建一个文件夹“templatetags”。在此文件夹中创建两个文件:

__init__.py

__init__.py

auth_extras.py

auth_extras.py

from django import template
from django.contrib.auth.models import Group 

register = template.Library()

@register.filter(name='has_group')
def has_group(user, group_name): 
    group = Group.objects.get(name=group_name) 
    return True if group in user.groups.all() else False

It should look like this now:

它现在应该是这样的:

app/
    __init__.py
    models.py
    templatetags/
        __init__.py
        auth_extras.py
    views.py

After adding the templatetags module, you will need to restart your server before you can use the tags or filters in templates.

添加模板标签模块后,您需要重新启动服务器,然后才能使用模板中的标签或过滤器。

In your base.html(template) use the following:

在您的base.html(模板)中使用以下内容:

{% load auth_extras %}

and to check if the user is in group "moderator":

并检查用户是否在“主持人”组中:

{% if request.user|has_group:"moderator" %} 
    <p>moderator</p> 
{% endif %}

Documentation: https://docs.djangoproject.com/en/1.11/howto/custom-template-tags/

文档:https: //docs.djangoproject.com/en/1.11/howto/custom-template-tags/

回答by Jaroslav Hájek

In your template

在您的模板中

{% ifequal user.groups.all.0.name "user" %}
  This is User
{% endifequal %}
  

回答by muccix

Watch out that you'll get an exception if the group does not exist in the DB.

请注意,如果该组不存在于数据库中,您将收到异常。

The custom template tag should be:

自定义模板标签应该是:

from django import template
from django.contrib.auth.models import Group

register = template.Library()

@register.filter(name='has_group')
def has_group(user, group_name):
    try:
        group =  Group.objects.get(name=group_name)
    except Group.DoesNotExist:
        return False

    return group in user.groups.all()

Your template:

您的模板:

{% if request.user|has_group:"mygroup" %} 
    <p>User belongs to my group 
{% else %}
    <p>User doesn't belong to mygroup</p>
{% endif %}

回答by Alex K.

I'd say that the best way is:

我想说最好的方法是:

yourapp/templatetags/templatetagname.py

yourapp/templatetags/templatetagname.py

from django import template

register = template.Library()

@register.filter(name='has_group')
def has_group(user, group_name):
    return user.groups.filter(name=group_name).exists()

yourapp/templates/yourapp/yourtemplate.html:

yourapp/templates/yourapp/yourtemplate.html:

{% load has_group %}

{% if request.user|has_group:"mygroup" %} 
    <p>User belongs to my group</p>
{% else %}
    <p>User does not belong to my group</p>
{% endif %}

EDIT: added line with template tag loading as was advised in comments.

编辑:按照评论中的建议添加了带有模板标签加载的行。

EDIT2: fixed minor typo.

EDIT2:修复了轻微的错字。

回答by Danut Popa

{% if target_group in user.groups.all.0.name %}
    # do your stuff
{% endif %}

回答by iqbal

Although the answer given by mishbah is right but it didn't work for me.

虽然 mishbah 给出的答案是正确的,但它对我不起作用。

I am using Django 2.2.7 and i figured out that register = template.Library()should be replaced with from django.template.defaultfilters import register.

我正在使用 Django 2.2.7,我发现register = template.Library()应该用from django.template.defaultfilters import register.

i hope someone will find it useful.

我希望有人会觉得它有用。