如果你创建一个 Template 对象,你可以用 context 来传递数据给它。 一个context是一系列变量和它们值的集合。
context在Django里表现为 Context 类,在django.template 模块里。 她的构造函数带有一个可选的参数:
一个字典映射变量和它们的值。 调用 Template 对象 的 render() 方法并传递context来填充模板
e.g:
>>> 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对象而不是普通的字符串
所以以上内容更好的讲解了以下知识
context = Context({
'to': phone,
'content': mark_safe(text)
})
resp = requests.post(sms_profile.url,data=json.loads(DjangoTemplate(sms_profile.params).render(context))) #短信网关对象参数
该功能可用于ckeditor占位符的渲染,如上就是将context中的文本替换成短信网关的url地址