Daan van der Kallen Daan van der Kallen - 4 years ago 266
Python Question

How to customize breadcrumbs in Django REST Framework Browsable API

Currently the Browsable API seems to derive the breadcrumbs from the classname of my views. However I am using nested resources for my API so I can get Breadcrumbs like:

Root / User List / User Detail / User Invite List / User Invite Detail

However the fact that the 'User Invite'-views show User again is pretty redundant. I would rather have that their breadcrumbs would just be 'Invite List' and 'Invite Detail'.

Now the most obvious solution would ofcourse be to just rename the view classes but that is not an option for me since I also have the classes ChatInviteList and ChatInviteDetail that share the same problem. So I can't name both of them InviteDetail and InviteList.

Is there a way to customize these breadcrumbs in another way? For example by setting some variable in the view class or something. I tried searching the docs but couldn't find anything.

Answer Source

Write a template called 'rest_framework/api.html' (docs) that extends 'rest_framework/base.html' in which you override the breadcrumbs block. For guidance, the original block looks:

{% block breadcrumbs %}
  <ul class="breadcrumb">
    {% for breadcrumb_name, breadcrumb_url in breadcrumblist %}
      {% if forloop.last %}
        <li class="active"><a href="{{ breadcrumb_url }}">{{ breadcrumb_name }}</a></li>
      {% else %}
        <li><a href="{{ breadcrumb_url }}">{{ breadcrumb_name }}</a></li>
      {% endif %}
    {% endfor %}
  </ul>
{% endblock %}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download