如何使用 Django 管理动态菜单?

How to manage a dynamic menu with Django?

我想根据类别模型动态创建菜单的 link。我使用 DetailView 和 ListView 创建单个类别的列表和详细信息页面,运行 很好。

现在我希望在 base.html 每次用户添加新类别时在 "dropdown" 菜单中看到一个新的 link。

这是base.html

{% load static %}

<!doctype html>
<html lang="en">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">

    <title>{% block head_title %}Test{% endblock head_title %}</title>
  </head>
  <body>


    <nav class="navbar navbar-expand-lg navbar-light bg-light">
      <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
      </button>

      <div class="collapse navbar-collapse" id="navbarSupportedContent">
        <ul class="navbar-nav mr-auto">
          <li class="nav-item active">
            <a class="nav-link" href="/">Home <span class="sr-only">(current)</span></a>
          </li>
          <li class="nav-item">
            <a class="nav-link" href="{% url 'list_tag' %}">Tag</a>
          </li>
          <li class="nav-item dropdown">
            <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
              Category
            </a>
            <div class="dropdown-menu" aria-labelledby="navbarDropdown">
              {% for category in category_list %}
                <a href="{{ category.get_absolute_url }}">{{ category.category_name }}</a>
              {% endfor %}
            </div>
          </li>
      </div>
    </nav>


    <div class="container">
      <!-- INIZIO corpo centrale -->
      {% block content %}

      {% endblock content %}
      <!-- FINE corpo centrale -->
    </div>

    <!-- Optional JavaScript -->
    <!-- jQuery first, then Popper.js, then Bootstrap JS -->
    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
    <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"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
  </body>
</html>

这是list_category.html

{% extends 'base.html' %}

{% load static %}

{% block head_title %}Elenco Categorie | {{ block.super }}{% endblock head_title %}

{% block content %}


  {% for category in category_list %}
    <h1><a href="{{ category.get_absolute_url }}">{{ category.category_name }}</a></h1>
    <hr>
  {% endfor %}


{% endblock content %}

这是views.py

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"

当我使用 base.html 中的代码时,显示的是一个空菜单。 我该如何解决?

UPDATE:

使用 中@ans2human 的解决方案,我创建了一个文件 context_processors.py,如下所示:

from .models import Category

def blog_menu(request):

    link_menu = Category.objects.all()
    return {
        'link_menu': link_menu
    }

然后我在 settings.py 中添加了字符串:

'blog.context_processors.blog_menu',

下拉菜单的新字符串是:

<li class="nav-item active dropdown">
  <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
    <i class="far fa-newspaper" id="img"></i> Blog
  </a>
  <div class="dropdown-menu" aria-labelledby="navbarDropdown">
    <a class="dropdown-item" href="#"></i> {{ link_menu }}</a>
  </div>
</li>

现在我在菜单中出现了这种情况

似乎有效但不正确。我该如何解决?

UPDATE 2

好消息! 由于我不是 Django 专家,所以我遇到了一个微不足道的困难。通过更多的推理,我找到了解决方案。

解决方案如下:

    <div class="dropdown-menu" aria-labelledby="navbarDropdown">
      {% for link in link_menu %}
        <a class="dropdown-item" href="{{ link.get_absolute_url }}"> {{ link.category_name }}</a>
      {% endfor %}
    </div>
def get_url(self):
    return reverse('product-by-category',args=[self.slug])
    
{% for menu in links %}
    <a class="dropdown-item" href="{{menu.get_url}}">{{menu.category_name}}</a>
{% endfor %}