来自Django模板的字符串,但没有返回HTTPResponse

SK9*_*SK9 1 django templates json

我有一个查询集,其中的对象我想用来填充模板.我结束的一种观点

return render_to_response('entry.json', {'entry_list':r}, mimetype="application/json; charset=utf-8")
Run Code Online (Sandbox Code Playgroud)

但是我希望能够使用这样的模板序列化到json,而不必返回HTTPResponse.在伪代码中,这可能是:

render('entry.json', {'entry_list':r}) #returns a string with the template entry.json
Run Code Online (Sandbox Code Playgroud)

这可能吗?如果是这样,怎么样?

Nit*_*mer 5

@HankGay所说的是正确的,尽管你有时可能想要获得模板响应而不返回HttpResponse,即使你正确使用Django.

阅读:渲染上下文:

>>> from django.template import Context, Template
>>> t = Template("My name is {{ my_name }}.")

>>> c = Context({"my_name": "Adrian"})
>>> t.render(c)
"My name is Adrian."

>>> c = Context({"my_name": "Dolores"})
>>> t.render(c)
"My name is Dolores."
Run Code Online (Sandbox Code Playgroud)

这就是你要追求的吗?