2017-07-12 114 views
1

的環境變量我有base.html在我的主模板index.html與頭_header_main.html文件I延伸base.html和包括_header_main.html但變數,我進入index.html不可見的父模板如何訪問家長模板Django的

基地。 HTML:

<html> 
    <head> 
     <title>{% block title %}{% endblock %}</title> 
     <!-- styles --> 
     ... 
    </head> 
    <body> 
     {% block _header_main %}{% endblock %} 
     {% block _header_mobile %}{% endblock %} 
     <div class="wrapper"> 
     <!-- header --> 
     {% block header_main %}{% endblock %} 
     {% block header_mobile %}{% endblock %} 

     <div class="after_header"></div> 
    <!-- end header --> 
    {% block content %}{% endblock %} 
     <footer class="footer"> 
      ... 
     </footer> 
    </div> 
    </body> 
</html> 

_header_main.html:

<header class="header"> 
    <ul> 
    {% for i in servers %} 
     <li>{{ i.title }}</li> 
    {% endfor %} 
    </ul> 
</header> 

的index.html:

{% extends "base.html" %} 
{% load static %} 
{% block title %}Title{% endblock %} 
{% block header_main %} 
    {% include "_header_main.html" %} 
{% endblock %} 
{% block content %} 
    Some content... 
{% endblock %} 
<scripts> 
... 
</scripts> 

我如何可以訪問到我的模板變量items

編輯。我在views.py

def index(request): 
    servers = Server.objects.all().order_by('ranking') 
    basket_count, total_price = basket_calculate(request) 
    server_arr = [i.title for i in servers] 
    servers_string = ', '.join(server_arr) 
    return render(request, 'index.html', {'servers':servers, 'temp_year' : datetime.now().year, 
              'basket_count':basket_count, 'total_price':total_price, 
              'servers_string':servers_string }) 
+0

請顯示您的看法。 – Alasdair

回答

0

之所以我items變量不模板顯示,功能是因爲我與js添加<script>標籤無遮擋,所以我的劇本沒有工作。我將我的腳本添加到{% block script %}並且所有工作正常!