我在这里使用这个django-plugin: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), '')
])
答案 0 :(得分: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 %}
我没有测试过这个;这就是我的快速代码检查所揭示的。
答案 1 :(得分: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)