0

I have a simple breadcrumb which is working great. But I want to add a feature, that every < li > tag is going to be displayed depending on the user's group.

So if the user is in group sample1, only the sample1 tag will be visible in the breadcrumb. Also, if the user is in the sample1 and sample2 group, both tag's will appear.

So I need something like this :

def bc(request):
    user_groups = request.user.groups.all()
    context = {'user_groups': user_groups}

    return render(request, 'opportunity/opp_breadcrumb.html', context)

opp.breadcrumb.html :

<ul id="bread" class="breadcrumb">
    {% for group in user_groups %}
        {% if group.name == 'Sample1' %}   
            <li class="breadcrumb-item"><a href="{% url 'opportunity:sample1' %}">Sample1</a></li>
        {% elif group.name == 'Sample2' %}
            <li class="breadcrumb-item"><a href="{% url 'opportunity:sample2' %}">Sample2</a></li>
        {% elif group.name == 'Sample3' %}
            <li class="breadcrumb-item"><a href="{% url 'opportunity:sample3' %}">Sample3</a></li>
        {% elif group.name == 'Sample4' %}
            <li class="breadcrumb-item"><a href="{% url 'opportunity:sample4' %}">Sample4</a></li>
        {% endif %}
    {% endfor %}

    <div class="ml-auto">
        <li style="margin-right: 20px">
            <a href="{% url 'opportunity:pdf' %}" target="_blank">
                <i class="fa fa-file-pdf-o" aria-hidden="true"></i>
            </a>
        </li>
    </div>
    <li class=" breadcrumb-item" style="margin-right: 20px"><a
            href="{% url 'opportunity:sample5' %}">Sample 5</a></li>
</ul>

But as you can suppose, this logic is not working at all.

dev.ink
  • 380
  • 5
  • 21
  • What do you mean by "it is not working at all"? Your ```div``` directly in your ```ul``` is not valid html (I think) and it might not be the best approach do loop through the groups in your template but the links should be displayed as desired. – Chris Jan 22 '20 at 17:05
  • I'm inclouding it into my desired page, and it works as needed. But when i try to loop and to show li tags depending on the users group, I get nothing. The breadcrumb disappears. And when i remove all the tags from django, it shows again. So the problem is behind the loop or something – dev.ink Jan 22 '20 at 17:10
  • Are you sure you current user actually belongs to any group? Try print the user_groups variable directly into your template (```{{ user_groups }}```) to check if it is empty – Chris Jan 22 '20 at 17:30
  • I can see at my admin panel that logged user belongs to two groups. I applied this same method for some other purpose, with the same user, and its working as desired. That's the main reason why I'm curious about this one, why it won't work?! – dev.ink Jan 22 '20 at 17:33
  • I tested the code you posted and it does work. So I strongly suspect that it must be some typo in group names or similar. Please try printing all the group names in your template. – Chris Jan 22 '20 at 17:45
  • I'll try it tomorrow, and I'll let you know – dev.ink Jan 22 '20 at 17:58

1 Answers1

0

I made a mistake, I was trying to render the user_groups to the wrong template. Since my opp_breadcrumb.html is included in my main template, I should just render it there instead using the bc view.

dev.ink
  • 380
  • 5
  • 21