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

Django源码分析——loader.py

时间:2015-09-08 12:07:22      阅读:124      评论:0      收藏:0      [点我收藏+]

标签:

from django.template import loader

 

 1 def render_to_string(template_name, dictionary=None, context_instance=None):
 2     """
 3     Loads the given template_name and renders it with the given dictionary as
 4     context. The template_name may be a string to load a single template using
 5     get_template, or it may be a tuple to use select_template to find one of
 6     the templates in the list. Returns a string.
 7     """
 8     dictionary = dictionary or {}
 9     if isinstance(template_name, (list, tuple)):
10         t = select_template(template_name)
11     else:
12         t = get_template(template_name)
13     if not context_instance:
14         return t.render(Context(dictionary))
15     # Add the dictionary to the context stack, ensuring it gets removed again
16     # to keep the context_instance in the same state it started in.
17     context_instance.update(dictionary)
18     try:
19         return t.render(context_instance)
20     finally:
21         context_instance.pop()

 

 注释:“”“

3加载给定template_name呈现与给定的字典
4上下文。template_name可能是字符串加载单个模板使用
5 get_template,或者它可能是一个元组使用select_template发现之一
6模板列表中。返回一个字符串。

”“”

三个参数:

template_name
dictionary=None
context_instance=None


8:dictionary = dictionary or {} 这句定义就不懂
9:如果template_name 类型 是列表,或者是元组的话,
就调用 select_template()
不然就调用 get_template(template_name)
13:如果没有 if not context_instance:
14:就放回 return t.render(Context(dictionary))

Django源码分析——loader.py

标签:

原文地址:http://www.cnblogs.com/IDomyself/p/4791082.html

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