码迷,mamicode.com
首页 > 其他好文 > 详细

Django:模板

时间:2018-10-30 17:43:21      阅读:196      评论:0      收藏:0      [点我收藏+]

标签:标签   schedule   aci   sch   inf   ice   逻辑判断   from   ant   

模板通常用于产生HTML,但是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>

用两个大括号括起来的文字(例如  {{ person_name }} )称为  变量 (variable) 。

被大括号和百分号包围的文本(例如 {% if ordered_warranty %} )是 模板标签 (template tag) 。标签(tag)定义比较明确,即: 仅通知模板系统完成某些工作的标签。

这个例子中的模板包含一个 for 标签( {% for item in item_list %} )和一个 if 标签( {% if ordered_warranty %} )

for标签类似Python的for语句,可让你循环访问序列里的每一个项目。 if 标签,正如你所料,是用来执行逻辑判断的。 在这里,tag标签检查ordered_warranty值是否为True。如果是,模板系统将显示{% ifordered_warranty %}和{% else %}之间的内容;否则将显示{% else %}和{% endif %}之间的内容。{% else %}是可选的。

最后,这个模板的第二段中有一个关于filter过滤器的例子,它是一种最便捷的转换变量输出格式的方式。如这个例子中的{{ship_date|date:”F j, Y” }},我们将变量ship_date传递给date过滤器,同时指定参数”Fj,Y”。date过滤器根据参数进行格式输出。

Django:模板

标签:标签   schedule   aci   sch   inf   ice   逻辑判断   from   ant   

原文地址:https://www.cnblogs.com/ywxbbbbb/p/9876830.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!