Loading

The Definitive Guide To Django 2 学习笔记(五) 第四章 模板 (一)基本模板系统

引入模板系统的原因,view中引入硬编码并非明智的选择,设计上的任何改变都会需要改动代码。python代码和HTML代码应该分开,这是多数Web站点的共识,分开会提高效率。

 

基本模板系统

Django模板是一串用来分离数据与文档模型的文本。参考下面的模板:

<html>
<head><title>Ordering notice</title></head>
<body>
<h1>Ordering notice</h1>
<p>Dear {{ person_name }},</p>
<p>Thanks for placing an order from {{ company }}. It's scheduled to
ship on {{ ship_date|date:"F j, Y" }}.</p>
<p>Here are the items you've ordered:</p>
<ul>
{% for item in item_list %}
<li>{{ item }}</li>
{% endfor %}
</ul>
{% if ordered_warranty %}
<p>Your warranty information will be included in the packaging.</p>
{% else %}
<p>You didn't order a warranty, so you're on your own when
the products inevitably stop working.</p>
{% endif %}
<p>Sincerely,<br />{{ company }}</p>
</body>
</html>

1.被双括号包围的是变量

2.被打括号和百分号包围的 称为 模板标签,实际上是判断逻辑

3.最后,在第二段中包含一个过滤器的例子,{{ship_date|date:"F j,Y"}}, 我们把ship_date 传给date过滤器,参数为F j,Y.此过滤器将时间格式化成一个给定的格式,过滤器用|来加载。

posted @ 2013-11-15 15:54  青岛欧姆网络科技  阅读(224)  评论(0编辑  收藏  举报