Search code examples
djangodjango-modelsdjango-usersdjango-taggingdjango-3.0

Django multiple User groups


Ok, so I have multiple users, and they belong to one, or more than one user group(I have 4 groups).

Each group has its own bootstrap card which shows on the HTML page.

How can I show 1,2,3 or more cards, depending on the user group?

If a user is in group1, I want to render card1, but if user is in group1 AND group2, I want to render card1 and card2, etc.

I tried {% if request.user.is_superuser %} and {%if 'group name' in user.groups.all.0.name %}, but that applies well only if a user is in one group.

Thanks.


Solution

  • get user groups from views.py

    def view(request):
        user_groups = request.user.groups.all()
    
        return render(request, 'app/temp.html',{'user_groups': user_groups})
    

    temp.html

    {% for group in user_groups %}
        {% if group.name=='Group1' %}
        <p>show card1</p>
        {% elif group.name=='Group2' %}
         <p>show card2</p>
        {% else %}
         <p>show card3</p>
    
    {% endfor %}