Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
扩展父django模板,以及包含一个变量_Django_Django Templates - Fatal编程技术网

扩展父django模板,以及包含一个变量

扩展父django模板,以及包含一个变量,django,django-templates,Django,Django Templates,我有一个基本模板,我正在尝试扩展。在该模板中,我有一个需要传递变量的导航 我所做的是通过使用一个include来“扩展”它,比如: {% include "base.html" with active_nav='atInventory' %} <nav class="col-xs-12 paddingVertical-sm"> <ul class="removePadding removeMargin txtXxs"> &

我有一个基本模板,我正在尝试扩展。在该模板中,我有一个需要传递变量的导航

我所做的是通过使用一个include来“扩展”它,比如:

{% include "base.html" with active_nav='atInventory' %}
    <nav class="col-xs-12 paddingVertical-sm">
        <ul class="removePadding removeMargin txtXxs">
            <li class="{% if active_nav == 'atContact' %} activeNav {% else %} inactiveNav{% endif %}">
                <a href="{% url 'contact' %}">CONTACT<a>
            </li>
            <li class="{% if active_nav == 'atAbout' %} activeNav {% else %} inactiveNav{% endif %}">
                <a href="{% url 'about' %}">ABOUT</a>
            </li>
            <li class="{% if active_nav == 'atProjects' %} activeNav {% else %} inactiveNav{% endif %}">
                <a href="{% url 'projects' %}">PROJECTS & CLINICS</a>
            </li>
            <li class="{% if active_nav == 'atServices' %} activeNav {% else %} inactiveNav{% endif %}">
                <a href="{% url 'services' %}">SERVICS</a>
            </li>
            <li class="{% if active_nav == 'atInventory' %}  activeNav {% else %} inactiveNav{% endif %}">
                <a href="{% url 'inventory' %}">INVENTORY</a>
            </li>
            <li class="{% if active_nav == 'atHome' %} activeNav {% else %} inactiveNav{% endif %}">
                <a href="{% url 'index' %}">HOME</a>
            </li>
            <br class="clear-fix">
        </ul>
        <img src="{% static 'images/assets/klossviolins_logo.png' %}" />
        <br class="clear-fix">
    </nav>
但这并没有以正确的顺序呈现具有扩展内容的基本块

导航设置在基座中,如下所示:

{% include "base.html" with active_nav='atInventory' %}
    <nav class="col-xs-12 paddingVertical-sm">
        <ul class="removePadding removeMargin txtXxs">
            <li class="{% if active_nav == 'atContact' %} activeNav {% else %} inactiveNav{% endif %}">
                <a href="{% url 'contact' %}">CONTACT<a>
            </li>
            <li class="{% if active_nav == 'atAbout' %} activeNav {% else %} inactiveNav{% endif %}">
                <a href="{% url 'about' %}">ABOUT</a>
            </li>
            <li class="{% if active_nav == 'atProjects' %} activeNav {% else %} inactiveNav{% endif %}">
                <a href="{% url 'projects' %}">PROJECTS & CLINICS</a>
            </li>
            <li class="{% if active_nav == 'atServices' %} activeNav {% else %} inactiveNav{% endif %}">
                <a href="{% url 'services' %}">SERVICS</a>
            </li>
            <li class="{% if active_nav == 'atInventory' %}  activeNav {% else %} inactiveNav{% endif %}">
                <a href="{% url 'inventory' %}">INVENTORY</a>
            </li>
            <li class="{% if active_nav == 'atHome' %} activeNav {% else %} inactiveNav{% endif %}">
                <a href="{% url 'index' %}">HOME</a>
            </li>
            <br class="clear-fix">
        </ul>
        <img src="{% static 'images/assets/klossviolins_logo.png' %}" />
        <br class="clear-fix">
    </nav>




有更好的方法吗?还是只需要稍微调整一下?提前感谢您在这方面提供的任何帮助。

我不会这样做-按预期的方式扩展它。Include用于包含可重用组件。在这里,您只希望每个模板使用{%extends'base.html%}扩展您的'base.html'

还有一个指针——我会用一个小脚本而不是大量的if语句来进行活动导航。因此,为每个li元素添加一个ID(在我的示例中为索引添加#nav home),在您的主页上有如下内容:

<script>
 $(document).ready(function() {
 $( "#nav-home").addClass("activeNav");
 });
</script>

$(文档).ready(函数(){
$(“#nav home”).addClass(“activeNav”);
});

您是在尝试包含模板还是扩展模板?什么是主模板?不要试图用包含来扩展…它破坏了逻辑的原则。。。为什么不简单地扩展它呢?或者为使用
活动导航的特殊区域创建一个块
是的,我在考虑使用一个块,然后在该块中使用包含?我试图扩展模板,它基本上只是顶部导航和页脚。