title: How to manage a dynamic menu with Django?
body: <p>I would like to create the links of the menu dynamically based on the Category models. I've used DetailView and ListView for create a list and detail page of a single category and it run fine.</p>
  
  <p>Now I would like to see in base.html a new link in a "dropdown" menu every time the user add a new Category.</p>
  
  <p>This is <strong>base.html</strong></p>
  
  <pre><code>{% load static %}
  
  &lt;!doctype html&gt;
  &lt;html lang="en"&gt;
    &lt;head&gt;
      &lt;!-- Required meta tags --&gt;
      &lt;meta charset="utf-8"&gt;
      &lt;meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"&gt;
  
      &lt;!-- Bootstrap CSS --&gt;
      &lt;link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous"&gt;
  
      &lt;title&gt;{% block head_title %}Test{% endblock head_title %}&lt;/title&gt;
    &lt;/head&gt;
    &lt;body&gt;
  
  
      &lt;nav class="navbar navbar-expand-lg navbar-light bg-light"&gt;
        &lt;button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation"&gt;
          &lt;span class="navbar-toggler-icon"&gt;&lt;/span&gt;
        &lt;/button&gt;
  
        &lt;div class="collapse navbar-collapse" id="navbarSupportedContent"&gt;
          &lt;ul class="navbar-nav mr-auto"&gt;
            &lt;li class="nav-item active"&gt;
              &lt;a class="nav-link" href="/"&gt;Home &lt;span class="sr-only"&gt;(current)&lt;/span&gt;&lt;/a&gt;
            &lt;/li&gt;
            &lt;li class="nav-item"&gt;
              &lt;a class="nav-link" href="{% url 'list_tag' %}"&gt;Tag&lt;/a&gt;
            &lt;/li&gt;
            &lt;li class="nav-item dropdown"&gt;
              &lt;a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"&gt;
                Category
              &lt;/a&gt;
              &lt;div class="dropdown-menu" aria-labelledby="navbarDropdown"&gt;
                {% for category in category_list %}
                  &lt;a href="{{ category.get_absolute_url }}"&gt;{{ category.category_name }}&lt;/a&gt;
                {% endfor %}
              &lt;/div&gt;
            &lt;/li&gt;
        &lt;/div&gt;
      &lt;/nav&gt;
  
  
      &lt;div class="container"&gt;
        &lt;!-- INIZIO corpo centrale --&gt;
        {% block content %}
  
        {% endblock content %}
        &lt;!-- FINE corpo centrale --&gt;
      &lt;/div&gt;
  
      &lt;!-- Optional JavaScript --&gt;
      &lt;!-- jQuery first, then Popper.js, then Bootstrap JS --&gt;
      &lt;script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"&gt;&lt;/script&gt;
      &lt;script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"&gt;&lt;/script&gt;
      &lt;script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"&gt;&lt;/script&gt;
    &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>This is <strong>list_category.html</strong></p>
  
  <pre><code>{% extends 'base.html' %}
  
  {% load static %}
  
  {% block head_title %}Elenco Categorie | {{ block.super }}{% endblock head_title %}
  
  {% block content %}
  
  
    {% for category in category_list %}
      &lt;h1&gt;&lt;a href="{{ category.get_absolute_url }}"&gt;{{ category.category_name }}&lt;/a&gt;&lt;/h1&gt;
      &lt;hr&gt;
    {% endfor %}
  
  
  {% endblock content %}
  </code></pre>
  
  <p>This is <strong>views.py</strong></p>
  
  <pre><code>class CategoryList(ListView):
      model = Category
      context_object_name = 'category_list'
      template_name = "list_category.html"
  
  
  class SingleCategory(DetailView):
      model = Category
      template_name = "single_category.html"
  </code></pre>
  
  <p>When I use the code in base.html it was shown an empty menu. 
  How I can resolve?</p>
  
  <blockquote>
    <p><strong><em>UPDATE:</em></strong></p>
  </blockquote>
  
  <p>Using the solution of @ans2human in <a href="https://stackoverflow.com/questions/51279768/bootstrap-navbar-dropdown-populated-with-database-entries-in-django-2">this post</a>, I've created a file <strong>context_processors.py</strong> like below:</p>
  
  <pre><code>from .models import Category
  
  def blog_menu(request):
  
      link_menu = Category.objects.all()
      return {
          'link_menu': link_menu
      }
  </code></pre>
  
  <p>Then I've added the string in <strong>settings.py</strong>:</p>
  
  <pre><code>'blog.context_processors.blog_menu',
  </code></pre>
  
  <p>The new strings of dropdown menu are these:</p>
  
  <pre><code>&lt;li class="nav-item active dropdown"&gt;
    &lt;a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"&gt;
      &lt;i class="far fa-newspaper" id="img"&gt;&lt;/i&gt; Blog
    &lt;/a&gt;
    &lt;div class="dropdown-menu" aria-labelledby="navbarDropdown"&gt;
      &lt;a class="dropdown-item" href="#"&gt;&lt;/i&gt; {{ link_menu }}&lt;/a&gt;
    &lt;/div&gt;
  &lt;/li&gt;
  </code></pre>
  
  <p>Now I've this situation in the menu <a href="https://i.stack.imgur.com/Mx5mT.png" rel="noreferrer"><img src="https://i.stack.imgur.com/Mx5mT.png" alt="enter image description here"></a></p>
  
  <p>It seems that  work but not correctly. How I can resolve?</p>