【问题标题】:django: side menu template visible everywheredjango:随处可见的侧边菜单模板
【发布时间】:2016-10-10 22:38:45
【问题描述】:

我需要在我的 django 管理员中创建一个应该出现在所有页面中的侧边菜单。理想情况下,我的模板应该是包含此代码并解析 app_list 参数的通用 navigation.html 文件(与用于填充仪表板的 index.html 模板相同):

{% block side_menu %}
{% if app_list %}
    {% for app in app_list %}
        <ul class="sidebar-menu app-{{ app.app_label }} module">
         <li class="treeview">
           <a href="#">
            <i class="fa fa-dashboard"></i> <span>{{ app.name | truncatechars:"25" }}</span>
            <span class="pull-right-container">
              <i class="fa fa-angle-left pull-right"></i>
            </span>
          </a>

        {% for model in app.models %}
        <ul class="treeview-menu">
            {% if model.admin_url %}
                <li><a href="{{ model.admin_url }}"><strong>{{ model.name }}</strong></a></li>
            {% else %}
                <li>{{ model.name }}</li>
            {% endif %}

            {% if model.add_url %}
                <li><a href="{{ model.add_url }}"><i class="fa fa-plus"></i> {% trans 'Add' %}</a></li>
            {% else %}
                <td>&nbsp;</td>
            {% endif %}

            {% if model.admin_url %}
                <li><a href="{{ model.admin_url }}"><i class="fa fa-pencil-square-o"></i> {% trans 'Change' %}</a></li>
            {% else %}
                <td>&nbsp;</td>
            {% endif %}
        </ul>
        {% endfor %}
        </li>
      </ul>
    {% endfor %}
{% else %}
    <p>{% trans "You don't have permission to edit anything." %}</p>
{% endif %}
{% endblock side_menu %}

我的问题是:

1) 如何在所有页面中包含新的 navigations.html 模板?

2) 如何将 app_list 字典传递给此模板?

提前感谢

【问题讨论】:

    标签: python django


    【解决方案1】:

    1) 它看起来像you're on the right track - 你有{% block %}。您只需要让您的其他文件扩展该导航文件(您可能只想将其设为 base.html,如链接中的文档,并使其包含所有页面应具有的所有内容)并允许这些页面添加自己的内容.

    navigations.html 允许一个主体:

    <html>
        {% block side_menu %}
            ...
        {% endblock side_menu %}
        <body>
            {% block body %}
            {% endblock body %}
        </body>
    </html>
    

    然后在每个模板的顶部:

    {% extends 'my_app/navigations.html' %}
    

    {% block body %} 中包含该模板内容。

    2) 您可以make a template context processor 将上下文传递给每个模板。根据文档,它们非常简单:

    这是一个 Python 函数,它接受一个参数,一个 HttpRequest 对象, 并返回一个添加到模板上下文的字典

    所以:

    def my_context_processor(request):
        apps = App.objects.all()
        return {'app_list': apps}
    

    在您的设置中:

    TEMPLATES = [
        {
            'BACKEND': 'django.template.backends.django.DjangoTemplates',
            'DIRS': [],
            'APP_DIRS': True,
            'OPTIONS': {
                'context_processors': [
                    # Other context processors....
                    'my_app.processors.my_context_processor',
                ],
            },
        },
    ]
    

    现在在每个模板中,您都可以执行 {% for app in app_list %} 而无需您的视图返回该上下文。

    【讨论】:

    • 非常感谢您详尽的回答,Jens。这正是我所需要的!
    猜你喜欢
    • 2021-01-22
    • 2017-04-12
    • 2016-09-03
    • 2015-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-22
    • 1970-01-01
    相关资源
    最近更新 更多