對於2子模板文件繼承塊時,{{ block.super }}
不能解決在Django中,{{block.super}}存在問題,我如何避免在多個模板文件中重複一個`block`?
的Python 2.5.2,Django的1.0中,Windows XP SP3
示例框架代碼所涉及的文件:
base.html
item_base.html
show_info_for_all_items.html
show_info_for_single_item.html
FILE:base.html
{% block content %}
{% endblock %}
FILE:item_base.html
{% extends "base.html" %}
{% block item_info %}
Item : {{ item.name }}<br/>
Price : {{ item.price }}<br/>
{% endblock %}
FILE:show_info_for_all_items.html
{% extends "item_base.html" %}
{% block content %}
<h1>info on all items</h1>
<hr/>
{% for item in items %}
{% block item_info %}
{{ block.super }}
{% endblock %}
<hr/>
{% endfor %}
{% endblock %}
FILE:show_info_for_single_item.html
{% extends "item_base.html" %}
{% block content %}
<h1>info on single item</h1>
{% block item_info %}
{{ block.super }}
{% endblock %}
{% endblock %}
show_info_for_all_items.html
顯示項目列表以及每個項目的信息。
show_info_for_single_item.html
顯示包含該項目信息的單個項目。用於顯示項目信息
show_info_for_all_items.html
和show_info_for_single_item.html
共享相同的代碼,所以我把它移到item_base.html
到block item_info
但show_info_for_all_items.html
的{{ block.super }}
和show_info_for_single_item.html
不起作用。 {{ block.super }}
解決爲空白。
如果我移動代碼回從block item_info
在item_base.html
爲show_info_for_all_items.html
和show_info_for_single_item.html
它的作品,但後來我有2個文件複製相同的block item_info
代碼。
如果block.super問題不能得到解決,不Django的提供的東西等包括=>{% INCLUDE "item_base.html" %}
所以從模板文件塊可以包括(而不是extends
)
如何避免在這兩個重複block item_info
html文件?
假如你的Django一派+包括你可能會問及在短的時間內回答了這個花了鍵入它in。 – 2008-11-22 01:31:54