zoukankan      html  css  js  c++  java
  • 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.此过滤器将时间格式化成一个给定的格式,过滤器用|来加载。

  • 相关阅读:
    C++程序设计入门--前言
    C++ string_view 的坑
    从OGRE,GAMEPLAY3D,COCOS2D-X看开源
    抽烟解闷的程序员
    一个团队应该是什么样
    准备开始接手公司的项目
    两位印象深刻的同事
    一段故事结束,一段生活开始
    starling性能优化总结(毫无疑问还是转载)
    知道端口号如何查看应用位置
  • 原文地址:https://www.cnblogs.com/kfx2007/p/3425582.html
Copyright © 2011-2022 走看看