简体   繁体   English

render 和 render_to_string 有什么区别?

[英]What is difference between render & render_to_string?

Currently I have referencing some old project created in django 1.9 version of my company and I have found that they have used render_to_string function many times as of now I am using render function but I have never used render_to_string.目前我参考了我公司的 django 1.9 版本中创建的一些旧项目,我发现他们已经多次使用 render_to_string 函数,现在我正在使用渲染函数,但我从未使用过 render_to_string。

Example code of render_to_string function is as below. render_to_string 函数的示例代码如下。

return HttpResponse(render_to_string('sales/sale.html', {'sale_id' : sale_id, `unique_number`:uni_id}, RequestContext(request))) 

I have tried to search online find answer but not able to find any perfect answer.我试图在网上搜索找到答案,但找不到任何完美的答案。

What is difference between both these function & how they behave ?这两个功能及其行为方式之间有什么区别?

When to decide which function is best suitable to use in project ?什么时候决定哪个功能最适合在项目中使用?

Thanks.谢谢。

We can check the source code of render [GitHub] :我们可以查看render [GitHub]源代码

 def render(request, template_name, context=None, content_type=None, status=None, using=None): """ Return a HttpResponse whose content is filled with the result of calling django.template.loader.render_to_string() with the passed arguments. """ content = loader. render_to_string( template_name, context, request, using=using ) return HttpResponse( content, content_type, status )

In essence render thus calls render_to_string(..) with the template_name , context , request , and using as parameters, and then constructs a HttpResponse with that result and optionally a content_type and status .本质上,因此rendertemplate_namecontextrequestusing作为参数调用render_to_string(..) ,然后使用该结果和可选的content_typestatus构造一个HttpResponse It is thus a shortcut that combines the two.因此,这是将两者结合起来的捷径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM