Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<h2>Something like this may work for your situation:</h2> <p>Capture the entire URL in your view and make links from it. This will require modifying your urls.py, each view that needs to have breadcrumbs, and your templates.</p> <p>First you would capture the entire URL in your urls.py file</p> original urls.py <pre><code>... (r'^myapp/$', 'myView'), (r'^myapp/(?P&lt;pk&gt;.+)/$', 'myOtherView'), ... </code></pre> new urls.py <pre><code>... (r'^(?P&lt;whole_url&gt;myapp/)$', 'myView'), (r'^(?P&lt;whole_url&gt;myapp/(?P&lt;pk&gt;.+)/)$', 'myOtherView'), ... </code></pre> <p>Then in your view something like:</p> views.py <pre><code>... def myView(request, whole_url): # dissect the url slugs = whole_url.split('/') # for each 'directory' in the url create a piece of bread breadcrumbs = [] url = '/' for slug in slugs: if slug != '': url = '%s%s/' % (url, slug) breadcrumb = { 'slug':slug, 'url':url } breadcrumbs.append(breadcrumb) objects = { 'breadcrumbs': breadcrumbs, } return render_to_response('myTemplate.html', objects) ... </code></pre> <p>Which should be pulled out into a function that gets imported into the views that need it</p> <p>Then in your template print out the breadcrumbs</p> myTemplate.html <pre><code>... &lt;div class="breadcrumb-nav"&gt; &lt;ul&gt; {% for breadcrumb in breadcrumbs %} &lt;li&gt;&lt;a href="{{ breadcrumb.url }}"&gt;{{ breadcrumb.slug }}&lt;/a&gt;&lt;/li&gt; {% endfor %} &lt;/ul&gt; &lt;/div&gt; ... </code></pre> <p>One shortcoming of doing it this way is that as it stands you can only show the 'directory' part of the url as the link text. One fix for this off the top of my head <em>(probably not a good one)</em> would be to keep a dictionary in the file that defines the breadcrumb function.</p> <p>Anyways that's <em>one</em> way you could accomplish breadcrumbs, cheers :)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload