zoukankan      html  css  js  c++  java
  • Python的Django框架中的Context使用

    Python的Django框架中的Context使用

    近期整理些Python方面的知识,一旦你创建一个 Template 对象,你能够用 context 来传递数据给它。 一个context是一系列变量和它们值的集合。


    context在Django里表现为 Context 类,在 django.template 模块里。 她的构造函数带有一个可选的參数: 一个字典映射变量和它们的值。 调用 Template 对象 的 render() 方法并传递context来填充模板,本文參考 http://www.aichengxu.com/view/60625

    >>> 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对象而不是普通的字符串。 假设你明确这样做给你带来了多大便利的话。尽可能地感激Django在幕后有条不紊地为你所做这这么多工作吧。

    假设不明确你从中获益了什么,别操心。你只须要知道Django对Unicode的支持。将让你的应用程序轻松地处理各式各样的字符集,而不不过主要的A-Z英文字符。

    字典和Contexts

    Python的字典数据类型就是keyword和它们值的一个映射。 Context 和字典非常类似, Context 还提供很多其它的功能。

    变量名必须由英文字符開始 (A-Z或a-z)并能够包括数字字符、下划线和小数点。

    (小数点在这里有特别的用途,稍后我们会讲到)变量是大写和小写敏感的。



    以下是编写模板并渲染的演示样例:

    >>> from django.template import Template, Context
    >>> raw_template = """<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>
    ...
    ... {% 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>"""
    >>> t = Template(raw_template)
    >>> import datetime
    >>> c = Context({'person_name': 'John Smith',
    ...   'company': 'Outdoor Equipment',
    ...   'ship_date': datetime.date(2009, 4, 2),
    ...   'ordered_warranty': False})
    >>> t.render(c)
    u"<p>Dear John Smith,</p>
    
    <p>Thanks for placing an order from Outdoor
    Equipment. It's scheduled to
    ship on April 2, 2009.</p>
    
    
    <p>You
    didn't order a warranty, so you're on your own when
    the products
    inevitably stop working.</p>
    
    
    <p>Sincerely,<br />Outdoor Equipment
    </p>"


    让我们逐步来分析下这段代码:

    首先我们导入 (import)类 Template 和 Context ,它们都在模块 django.template 里。

    我们把模板原始文本保存到变量 raw_template 。注意到我们使用了三个引號来 标识这些文本,由于这样能够包括多行。



    接下来,我们创建了一个模板对象 t ,把 raw_template 作为 Template 类构造函数的參数。



    我们从Python的标准库导入 datetime 模块,以后我们将会使用它。



    然后。我们创建一个 Context 对象。 c 。

    Context 构造的參数是Python 字典数据类型。 在这里,我们指定參数 person_name 的值是 'John Smith' , 參数company 的值为 ‘Outdoor Equipment' ,等等。

    最后。我们在模板对象上调用 render() 方法,传递 context參数给它。

    这是返回渲染后的模板的方法,它会替换模板变量为真实的值和运行块标签。

    注意,warranty paragraph显示是由于 ordered_warranty 的值为 True . 注意时间的显示, April 2, 2009 , 它是按 'F j, Y' 格式显示的。

    假设你是Python刚開始学习的人,你可能在想为什么输出里有回车换行的字符(' ' )而不是 显示回车换行? 由于这是Python交互解释器的缘故: 调用 t.render(c) 返回字符串, 解释器缺省显示这些字符串的 真实内容呈现 ,而不是打印这个变量的值。 要显示换行而不是 ' ' 。使用 print 语句: print t.render(c) 。

    这就是使用Django模板系统的基本规则: 写模板,创建 Template 对象。创建 Context 。 调用 render() 方法。



    同一模板。多个上下文

    一旦有了 模板 对象,你就能够通过它渲染多个context, 比如:

    >>> from django.template import Template, Context
    >>> t = Template('Hello, {{ name }}')
    >>> print t.render(Context({'name': 'John'}))
    Hello, John
    >>> print t.render(Context({'name': 'Julie'}))
    Hello, Julie
    >>> print t.render(Context({'name': 'Pat'}))
    Hello, Pat


    不管何时我们都能够像这样使用同一模板源渲染多个context。仅仅进行 一次模板创建然后多次调用render()方法渲染会更为高效:

    # Bad
    for name in ('John', 'Julie', 'Pat'):
      t = Template('Hello, {{ name }}')
      print t.render(Context({'name': name}))
    
    # Good
    t = Template('Hello, {{ name }}')
    for name in ('John', 'Julie', 'Pat'):
      print t.render(Context({'name': name}))


    Django 模板解析很快捷。 大部分的解析工作都是在后台通过对简短正則表達式一次性调用来完毕。 这和基于 XML 的模板引擎形成鲜明对照,那些引擎承担了 XML 解析器的开销,且往往比 Django 模板渲染引擎要慢上几个数量级。 

  • 相关阅读:
    3.21上午
    3.17下午
    2017.4.14-morning
    2017.4.13-afternoon
    2017.4.13-morning
    2017.4.12-afternoon
    2017.4.12-morning
    2017.4.11-afternoon
    2017.4.11-morning
    2017.4.10-afternoon
  • 原文地址:https://www.cnblogs.com/blfbuaa/p/6772700.html
Copyright © 2011-2022 走看看