How to implement breadcrumbs in a Django template?

前端 未结 12 2057
無奈伤痛
無奈伤痛 2020-12-07 09:45

Some solutions provided on doing a Google search for \"Django breadcrumbs\" include using templates and block.super, basically just extending the base blocks and adding the

相关标签:
12条回答
  • 2020-12-07 10:08

    I had the same issue and finally I've made simple django tempalate tag for it: https://github.com/prymitive/bootstrap-breadcrumbs

    0 讨论(0)
  • 2020-12-07 10:11

    Try django-breadcrumbs — a pluggable middleware that add a breadcrumbs callable/iterable in your request object.

    It supports simple views, generic views and Django FlatPages app.

    0 讨论(0)
  • 2020-12-07 10:12

    Try django-mptt.

    Utilities for implementing Modified Preorder Tree Traversal (MPTT) with your Django Model classes and working with trees of Model instances.

    0 讨论(0)
  • 2020-12-07 10:12

    Obviously, no one best answer, but for practical reason I find that it is worth considering the naïve way. Just overwrite and rewrite the whole breadcrumb... (at least until the official django.contrib.breadcrumb released )

    Without being too fancy, it is better to keep things simple. It helps the newcomer to understand. It is extremely customizable (e.g. permission checking, breadcrumb icon, separator characters, active breadcrumb, etc...)

    Base Template

    <!-- File: base.html -->
    <html>
    <body>
      {% block breadcrumb %}
      <ul class="breadcrumb">
        <li><a href="{% url 'dashboard:index' %}">Dashboard</a></li>
      </ul>
      {% endblock breadcrumb %}
      {% block content %}{% endblock content %}
    </body>
    </html>
    

    Implementation Template

    Later on each pages we rewrite and overwrite the whole breadcrumb block.

    <!-- File: page.html -->
    {% extends 'base.html' %}
    {% block breadcrumb %}
    <ul class="breadcrumb">
      <li><a href="{% url 'dashboard:index' %}">Dashboard</a></li>
      <li><a href="{% url 'dashboard:level-1:index' %}">Level 1</a></li>
      <li class="active">Level 2</li>
    </ul>
    {% endblock breadcrumb %}
    

    Practicallity

    Realworld use cases:

    • Django Oscar: base template, simple bread
    • Django Admin: base template, simple bread, permission check breadcrumb
    0 讨论(0)
  • 2020-12-07 10:14

    You might want to try django-headcrumbs (don’t worry, they are not going to eat your brains).

    It’s very lightweight and absolutely straightforward to use, all you have to do is annotate your views (because defining crumbs structure in templates sounds crazy to me) with a decorator that explains how to get back from the given view.

    Here is an example from the documentation:

    from headcrumbs.decorators import crumb
    from headcrumbs.util import name_from_pk
    
    @crumb('Staff')  # This is the root crumb -- it doesn’t have a parent
    def index(request):
        # In our example you’ll fetch the list of divisions (from a database)
        # and output it.
    
    @crumb(name_from_pk(Division), parent=index)
    def division(request, slug):
        # Here you find all employees from the given division
        # and list them.
    

    There are also some utility functions (e.g. name_from_pk you can see in the example) that automagically generate nice names for your crumbs without you having to wright lots of code.

    0 讨论(0)
  • 2020-12-07 10:15

    http://www.djangosnippets.org/snippets/1289/ - provides a template tag but i'm not sure this would work if you don't have your urls.py properly declared.

    Nothing will work if you don't have your urls.py properly declared. Having said that, it doesn't look as though it imports from urls.py. In fact, it looks like to properly use that tag, you still have to pass the template some variables. Okay, that's not quite true: indirectly through the default url tag, which the breadcrumb tag calls. But as far as I can figure, it doesn't even actually call that tag; all occurrences of url are locally created variables.

    But I'm no expert at parsing template tag definitions. So say somewhere else in the code it magically replicates the functionality of the url tag. The usage seems to be that you pass in arguments to a reverse lookup. Again, no matter what your project is, you urls.py should be configured so that any view can be reached with a reverse lookup. This is especially true with breadcrumbs. Think about it:

    home > accounts > my account
    

    Should accounts, ever hold an arbitrary, hardcoded url? Could "my account" ever hold an arbitrary, hardcoded url? Some way, somehow you're going to write breadcrumbs in such a way that your urls.py gets reversed. That's really only going to happen in one of two places: in your view, with a call to reverse, or in the template, with a call to a template tag that mimics the functionality of reverse. There may be reasons to prefer the former over the latter (into which the linked snippet locks you), but avoiding a logical configuration of your urls.py file is not one of them.

    0 讨论(0)
提交回复
热议问题