• Django的模板系统


    1.一个简单的模板例子:

    <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 %} )是 模板标签
    • filter过滤器的例子,它是一种最便捷的转换变量输出格式的方式。 如这个例子中的{{ship_date|date:”F j, Y” }},我们将变量ship_date传递给date过滤器,同时指定参数”F j,Y”。date过滤器根据参数进行格式输出.

    2.模板系统工作原理

    • 在Python代码中使用Django模板的最基本方式如下:
      1. 可以用原始的模板代码字符串创建一个 Template 对象, Django同样支持用指定模板文件路径的方式来创建 Template 对象;
      2. 调用模板对象的render方法,并且传入一套变量context。它将返回一个基于模板的展现字符串,模板中的变量和标签会被context值替换; 

    • >>> from django import template
      >>> t = template.Template('My name is {{ name }}.')
      >>> c = template.Context({'name': 'Adrian'})
      >>> print t.render(c)
      My name is Adrian.
      >>> c = template.Context({'name': 'Fred'})
      >>> print t.render(c)
      My name is Fred.
      • 系统会在下面的情形抛出 TemplateSyntaxError 异常:

        • 无效的tags
        • 标签的参数无效
        • 无效的过滤器
        • 过滤器的参数无效
        • 无效的模板语法
        • 未封闭的块标签 (针对需要封闭的块标签)

    3.模板的渲染

      • 一旦你创建一个 Template 对象,你可以用 context 来传递数据给它。 一个context 是一系列变量和它们值的集合
      • context 在 Django 里表现为 Context 类,在 django.template 模块里。它的构造函数带有一个可选的参数: 一个字典映射变量和它们的值。 调用 Template 对象 的 render() 方法并传递 context 来填充模板:
      • >>> from django.template import Context, Template
        >>> t = Template('My name is {{ name }}.')
        >>> c = Context({'name': 'Stephane'})
        >>> t.render(c)
        u'My name is Stephane.'

        t.render(c) 返回的值是一个 Unicode 对象,不是普通的 Python 字符串。 你可以通过字符串前的 u 来区分。 在框架中,Django 会一直使用 Unicode 对象而不是普通的字符串。

      • 字典和Contexts

        • 变量名必须由英文字符开始 (A-Z或a-z)并可以包含数字字符、下划线和小数点。 (小数点在这里有特别的用途,稍后我们会讲到)变量是大小写敏感的
        • 在 Django 模板中遍历复杂数据结构的关键是句点字符 (.)。

          最好是用几个例子来说明一下。 比如,假设你要向模板传递一个 Python 字典。 要通过字典键访问该字典的值,可使用一个句点:

        • >>> from django.template import Template, Context
          >>> person = {'name': 'Sally', 'age': '43'}
          >>> t = Template('{{ person.name }} is {{ person.age }} years old.')
          >>> c = Context({'person': person})
          >>> t.render(c)
          u'Sally is 43 years old.'

    --------------------------------------------------

    4.用render_to_response()简化views层

    简化操作,不同加载模板那个过程render_to_response() 的第一个参数必须是要使用的模板名称。 如果要给定第二个参数,那么该参数必须是为该模板创建 Context 时所使用的字典。 如果不提供第二个参数, render_to_response() 使用一个空字典。

  • 相关阅读:
    SCU 3133(博弈)
    SCU 3132(博弈)
    hdu 5183(hash)
    hdu3329(2次dfs)
    hdu5179(数位dp)
    zoj2314(有上下界的网络流)
    CF 519E(树上倍增求lca)
    hdu1251(Trie树)
    SCU 2009(数位dp)
    【Leetcode】Letter Combinations of a Phone Number
  • 原文地址:https://www.cnblogs.com/zzblee/p/4725004.html
Copyright © 2020-2023  润新知