简体   繁体   English

面包屑动态内容

[英]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.但我想添加一个功能,即每个 < li > 标签都将根据用户组显示。

So if the user is in group sample1, only the sample1 tag will be visible in the breadcrumb.因此,如果用户在组 sample1 中,则面包屑中只会显示 sample1 标签。 Also, if the user is in the sample1 and sample2 group, both tag's will appear.此外,如果用户在 sample1 和 sample2 组中,则两个标签都会出现。

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: 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.但是正如您所想的那样,这种逻辑根本行不通。

I made a mistake, I was trying to render the user_groups to the wrong template.我犯了一个错误,我试图将 user_groups 渲染到错误的模板。 Since my opp_breadcrumb.html is included in my main template, I should just render it there instead using the bc view.由于我的 opp_breadcrumb.html 包含在我的主模板中,我应该只在那里渲染它而不是使用 bc 视图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM