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

时间:2018-11-13 10:06:47

标签: html django django-templates django-views django-2.1

我想基于类别模型动态创建菜单链接。我已经使用DetailView和ListView来创建单个类别的列表和详细信息页面,并且运行良好。

现在,我希望每次用户添加新类别时都在base.html中的“下拉菜单”中看到一个新链接。

这是 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中的代码时,显示了一个空菜单。 我该如何解决?

  

更新:

使用this post中的@ 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>

现在我在菜单enter image description here中遇到了这种情况

似乎可以正常工作,但不能正常工作。我该如何解决?

1 个答案:

答案 0 :(得分:1)

  

更新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>