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

后端 未结 9 1876
广开言路
广开言路 2020-12-07 20:38

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

It is possible in a view which is generating the template but what if I want

9条回答
  •  佛祖请我去吃肉
    2020-12-07 21:04

    You can use this:

    {% for group_for in request.user.groups.all %}
        {% if group_for.name == 'Customers' %}
            Text showed to users in group 'Customers'
        {% elif group_for.name == 'Sellers' %}
            Text showed to users in group 'Sellers'
        {% endif %}
    {% endfor %}
    

    This is iterating through groups related to the user who makes the request and printing the text if the name of the iterated group equals 'Customers', 'Sellers', etc

提交回复
热议问题