【问题标题】:Change to my left nav bar when on a certain page?在某个页面上更改为我的左侧导航栏?
【发布时间】:2012-02-17 11:00:07
【问题描述】:

也许我的做法完全错误......但我想做的是通过我所在的页面更改左侧导航。 base.html 中的左侧导航自始至终都很明显,但是一旦用户在 forums.html(扩展 base.html)上,我想更改左侧导航。

base.html:

{% if not no_left_bar %}
    <div class="container">
        <div class="row">
        <!-- Left Side Bar -->
             <nav>
                  original base.html nav goes here
             </nav>
             <!-- Some condition that goes here/ When forums.html -->
             <nav>
                  forums.html( extends base.html ) nav goes here
             </nav>
         </div>
     </div>
{% endif %}

我不知道我是否必须通过基本上下文传递它。感谢您的帮助和任何想法/建议。

【问题讨论】:

    标签: django html django-templates django-context


    【解决方案1】:

    django {% block %} 模板标签允许您定义可以被子模板覆盖的内容块。试试这样的:

    base.html

    <div class="container">
        <div class="row">
            {% block nav %}
                 <nav>
                      <!-- original base.html nav goes here -->
                 </nav>
             {% endblock %}
         </div>
     </div>
    

    然后在 forums.html

    {% extends "base.html" %}
    
    {% block nav %}
    
        <nav>
            <!-- new forums.html nav goes here -->
        </nav>
    
    {% endblock %}
    

    输出看起来像

    <div class="container">
        <div class="row">
                <nav>
                    <!-- new forums.html nav goes here -->
                </nav>
         </div>
     </div>
    

    此处的文档:https://docs.djangoproject.com/en/dev/topics/templates/#template-inheritance

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-05
      • 2018-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多