django-simple-menu与bootstrap的集成

时间:2016-09-05 14:16:31

标签: html css django twitter-bootstrap

我正在尝试将django第三方应用与引导模板集成。此第三方应用程序为网站提供动态生成的菜单。这个应用程序叫做django-simple-menu。以下是从Django模板访问菜单的代码,它运行良好。

base.html文件

{% load menu %}{% generate_menu %}
<!doctype html>
<html>
    <head>
        <title>{% block title %}No title set{% endblock %}</title>

        {% block headextra %}{% endblock %}
    </head>

    <body>
        <h1>User Menu</h1>
        {% with menu=menus.user %}{% include "menu.html" %}{% endwith %}

        <h1>Main Menu</h1>
        {% with menu=menus.main %}{% include "menu.html" %}{% endwith %}

        <h1>Example Menu</h1>
        {% with menu=menus.ExampleMenu %}{% include "menu.html" %}{% endwith %}

        <hr>

        {% block content %}
        {% endblock %}
    </body>
</html>

menu.html

<ul>
    {% for item in menu %}
    {% if item.separator %}<li>------------</li>{% endif %}
    <li><a href="{{ item.url }}">{{ item.title }}</a></li>
            {% for child in item.children %}
                <li><a href="{{ child.url }}">{{ child.title }}</a></li>
            {% endfor %}
     <br> <br>
    {% endfor %}
</ul>

我正在尝试将它与引导程序中的以下位置集成,它提供了一个静态导航栏。我希望它动态填充。任何帮助都会很棒。我不熟悉CSS,对HTML和JS的经验很少。

 <!-- Static navbar -->
    <nav class="navbar navbar-default navbar-static-top">
      <div class="container">
        <div class="navbar-header">
          <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
          <a class="navbar-brand" href="#">Project name</a>
        </div>
        <div id="navbar" class="navbar-collapse collapse">
          <ul class="nav navbar-nav">
            <li class="active"><a href="#">Home</a></li>
            <li><a href="#about">About</a></li>
            <li><a href="#contact">Contact</a></li>
            <li class="dropdown">
              <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Dropdown <span class="caret"></span></a>
              <ul class="dropdown-menu">
                <li><a href="#">Action</a></li>
                <li><a href="#">Another action</a></li>
                <li><a href="#">Something else here</a></li>
                <li role="separator" class="divider"></li>
                <li class="dropdown-header">Nav header</li>
                <li><a href="#">Separated link</a></li>
                <li><a href="#">One more separated link</a></li>
              </ul>
            </li>
          </ul>
          <ul class="nav navbar-nav navbar-right">
            <li><a href="../navbar/">Default</a></li>
            <li class="active"><a href="./">Static top <span class="sr-only">(current)</span></a></li>
            <li><a href="../navbar-fixed-top/">Fixed top</a></li>
          </ul>
        </div><!--/.nav-collapse -->
      </div>
    </nav>

我处于只有下拉菜单不起作用的阶段。我能够整合普通菜单。下面是我的代码,如果有人能告诉我我做错了什么会很棒。

<nav class="navbar navbar-default">
        <div class="container-fluid">
          <div class="navbar-header">
            <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
              <span class="sr-only">Toggle navigation</span>
              <span class="icon-bar"></span>
              <span class="icon-bar"></span>
              <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand" href="#">Project name</a>
          </div>

          <div id="navbar" class="navbar-collapse collapse">

            <ul class="nav navbar-nav">
                {% for item in menu %}
                    {#% if item.visible %#}
                        {% if item.separator %}<li class="divider-vertical"></li>{% endif %}
                        <li class="{% if item.selected %} active{% endif %}{% if item.children %} dropdown{% endif %}" id="top-menu-{{ item.slug }}">
                        {% if item.children %}
                            <a href="{{ item.url }}" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false" >
                        {% else %}
                            <a href="{{ item.url }}">
                        {% endif %}
                            {% if item.icon %}  <i class="icon-{{ item.icon }} icon-white"></i>&nbsp;  {% endif %}
                            {{ item.title }}
                        {% if item.children %} <span class="caret"></span> {% endif %}
                            </a>
                        {% if item.children %}
                                <ul class="dropdown-menu">
                                    {% for child in item.children %}
                                        <li><a href="{{ child.url }}">{{ child.title }}</a></li>
                                    {% endfor %}
                                </ul>
                        {% endif %}
                        </li>
                    {#% endif %#}
                {% endfor %}
            </ul>

            </div><!--/.nav-collapse -->
    </div><!--/.container-fluid -->
</nav>

我想知道是否有人可以指导我如何在这个for循环中集成菜单,而不会失去很棒的自举项目的外观和感觉。

感谢。

1 个答案:

答案 0 :(得分:1)

您必须制作新模板并将其包含在{% if item.children %}标记之后。类似的东西:

<li class="{% if item.selected %}active{% endif %} {% if item.children %}dropdown dropdown-toggle{% endif %}">
    <a href="{{ item.url }}">
        {{item.title}}
    </a>
    {% if item.children %}
        {% include 'base/navbar-sub.html' with items=item.children %}
    {% endif %}
</li>

然后,你的navbar-sub会是这样的:

{% load menu %}
<ul class="dropdown-menu">
    {% generate_menu %}
    {% for item in items %}
        {% if item.visible %}
            <li class="{% if item.selected %}active{% endif %}">
                <a href="{{ item.url }}">
                {{item.title}}
                </a>
            </li>
        {% endif %}
    {% endfor %}
</ul>