-1

我在这里使用这个 django 插件:https ://github.com/chronossc/django-breadcrumbs

但是,如果实际上有一系列面包屑,我似乎无法检查模板……我试过了:

{%if request.breadcrumbs%} {% if request.breadcrumbs|length > 0%} 

等等……但没有什么能真正告诉我是否有一些。

如果我通过面包屑,我希望这个栏出现,而不是在我没有通过一些面包屑的情况下出现:

{% if request.breadcrumbs|length > 0 %}
<div class="navbar navbar-fixed-top" style="top:38px;z-index:1029;" id="breadcrumb-sticky-header">
    <div class="container">
        <div class="container-fluid">
            <div class="row-fluid">
                <ul id="breadcrumb-sticky-header-inner" class="breadcrumb">
                    {% for breadcrumb in request.breadcrumbs %}
                    {% if not forloop.last %}
                    <li><a href="{{ breadcrumb.url }}">{{ breadcrumb.name }}</a> <span class="divider">/</span></li> 
                    {% else %}
                    <li class="active">{{ breadcrumb.name }}</li> 
                    {% endif %}
                    {% endfor %}
                </ul>
            </div>
        </div>
    </div>
</div>
{% endif %}

这是我认为的代码:

@login_required
def view(request, t_id):
    try:
        tshoot = Troubleshoot.objects.select_related('category', 'equipment', 'equipment__model').get(pk=t_id)
        request.breadcrumbs([
            (("%s: %s" % (tshoot.equipment.model.name, tshoot.equipment.serial)),
                '/equipment/view/%s/' % (tshoot.equipment.id)),
            (("%s" % (tshoot.category.name)),
                '/troubleshoot/categories/view/%s/' % (tshoot.category.id)),
            ((tshoot.title), '')
        ])
4

2 回答 2

1

基于 django-breadcrumbs 项目中的代码(breadcrumbs.py 中的第 191 行: https ://github.com/chronossc/django-breadcrumbs/blob/master/breadcrumbs/breadcrumbs.py#L191 ),看起来他们'已经实现了一个.all()方法。

您应该能够对方法|length的结果使用模板过滤器.all()来获得所需的结果,如下所示:

{% if request.breadcrumbs and request.breadcrumbs.all|length > 0 %}

我没有测试过这个;这就是我的快速代码检查所揭示的。

于 2012-11-06T18:26:02.343 回答
0

不确定是什么request.breadcrumbs|length > 0,但我会使用示例模板:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""
"http://www.w3.org/TR/html4/strict.dtd">
<html>
    <head>
        <meta http-equiv="content-type" content="text/html; charset=utf-8">

        <title>Breadcrumbs test page | {% for b in request.breadcrumbs %}{{ b.name }}{% if not forloop.last %} > {% endif %}{% endfor %}</title>

    </head>
    <body>
        {{ request.breadcrumbs }}
        <p>Breadcrumb: {% for b in request.breadcrumbs %}<a href="{{ b.url }}">{{ b.name }}</a>{% if not forloop.last %} / {% endif %}{% endfor %}</p>
        <p>Links: <a href='/'>Home</a> | <a href='/someview/'>Breadcrumb in view</a> | <a href='/flat01/'>Flatpages</a>
        <p>Content: <br>
        {% if text %}{{ text }}{% endif %}
        {% if flatpage %}{{ flatpage.content }}{% endif %}
    </body>
</html>

并查看直到您对它的工作方式感到满意:

# Create your views here.

from django.shortcuts import render_to_response
from django.template.context import RequestContext


def home(request):
    print request.breadcrumbs
    return render_to_response('home.html',
        {'text': 'Hello, this is home!'},
        context_instance=RequestContext(request))


def someview(request):
    request.breadcrumbs('just a view to show some url', request.path)

    return render_to_response('home.html',
        {'text': 'Hello, this is some second view'},
        context_instance=RequestContext(request))

(来自:https ://github.com/chronossc/django-breadcrumbs/blob/master/breadcrumbs_sample )

于 2012-11-06T18:25:12.140 回答