nu *_*est 0 django django-templates django-views
在 Django v1.4.3 下
为什么下面的 Django 模板案例 1 中的 if 语句总是显示块语句的内容,而与 if 语句是否为 TRUE 无关?
模板中的块语句是否总是在 if 语句之前执行?(也许我在文档中错过了这一点。)
View.py(请注意,为了测试目的故意“不”提供map_url):
def post_address(request):
return render_to_response(
'record/post_address.html',
{'form': form},
context_instance=RequestContext(request)
)
Run Code Online (Sandbox Code Playgroud)
base_integrated_form.html 父模板包含
{% block after_form %}
{% endblock after_form %}
Run Code Online (Sandbox Code Playgroud)
post_address.html(两种情况)Django模板案例1:(将块语句嵌套在if语句中将导致块语句的内容始终显示在浏览器中,与是否map_url提供或NOT无关。)
{% extends "base_integrated_form.html" %}
{% if map_url %}
{% block after_form %}
<div style="max-width:555px; height:240px; margin-left:auto; margin-right:auto;">
<iframe id="map" width="100%" height="100%" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="{{ map_url }}" style="border: 0px solid black"></iframe>
</div>
{% endblock after_form %}
{% endif %}
Run Code Online (Sandbox Code Playgroud)
Django模板案例2(在块语句中嵌套if语句仅在map_url提供的情况下在浏览器中显示块语句的内容。):
{% extends "base_integrated_form.html" %}
{% block after_form %}
{% if map_url %}
<div style="max-width:555px; height:240px; margin-left:auto; margin-right:auto;">
<iframe id="map" width="100%" height="100%" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="{{ map_url }}" style="border: 0px solid black"></iframe>
</div>
{% endif %}
{% endblock after_form %}
Run Code Online (Sandbox Code Playgroud)
块标签与其他模板标签不同,它们是模板继承系统的核心部分。
本质上,块标签在模板最初加载时(渲染之前)进行评估,以便 Django 可以查找继承链并构建最终的模板对象。这意味着其他标签内的块(例如“if”)将不会按您期望的方式工作,因为一旦真正的模板渲染开始,块标签就不再存在。
| 归档时间: |
|
| 查看次数: |
223 次 |
| 最近记录: |