[Fixed]-Django template extension: How do I best build a basenavbar that is also modifiable?

1👍

If you pull the whole nav-bar section out into its own block, you can override that only in the template you need to.

<!-- base.html -->
{% block navbar %}
<div class="col-sm-3 col-md-2 sidebar">
  <ul class="nav nav-sidebar">

    <li><a href="/list-view" class="active">All objects of model</a></li>
    <li><a href="/create-view">Create new object of model</a></li>

  </ul>
</div>
{% endblock %}

<!-- list.html -->
{% block navbar %}
<div class="col-sm-3 col-md-2 sidebar">
  <ul class="nav nav-sidebar">

    <li><a href="/list-view" class="active">All objects of model</a></li>
    <li><a href="/create-view">Create new object of model</a></li>
    <li>My filter thingie</li>
  </ul>
</div>
{% endblock %}

On the other hand, if you’re happy for extra items to always go at the end, it would be better to – as you say – just put an empty block in the base template after the last of the site-wide nav items, and override that. (Although I’d maybe make the name more generic than filter_thingie, anticipating the inevitable occasion when something else needs to go in that slot…)

Leave a comment