标签:
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))
标签:
原文地址:http://www.cnblogs.com/IDomyself/p/4791082.html