Search code examples
djangodjango-modelsdjango-templatesdjango-viewsdjango-tagging

Breadcrumb dynamic content


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.


Solution

  • 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.