Django - render(),render_to_response()和direct_to_template()之间的区别是什么? [英] Django - what is the difference between render(), render_to_response() and direct_to_template()?
问题描述
render()
, render_to_response()$之间的视图中,有什么区别(python / django noob可以理解的语言) c $ c>和 direct_to_template()
?
eg来自 Nathan Borror的基本应用示例
def comment_edit(request,object_id,template_name ='comments / edit.html'):
comment = get_object_or_404(Comment,pk = object_id,user = request.user)
#...
return render(request,template_name,{
'form':form,
'comment':comment,
})
但我也看过
return render_to_response(template_name,my_data_dictionary,
context_instance = RequestContext(request))
和
return direct_to_template(request,template_name,my_data_dictionary)
有什么区别,在任何特定情况下使用什么?
解决方案 https://docs.djangoproject.com/en/1.8/topics/http/shortcuts/#render
render(request,template [,dictionary] [,context_instance] [,content_type] [,status] [,current_app])
render()
是一个打破新$ {code> render_to_response
in 1.3将自动使用我现在最肯定会使用的 RequestContext
。 https://docs.djangoproject。 com / en / 1.8 / topics / http / shortcuts /#render-to-response
render_to_response(template [ ,字典] [,context_instance] [,mimetype])¶
render_to_response
是您在教程中使用的标准渲染功能。要使用 RequestContext
,您必须指定 context_instance = RequestContext(request)
direct_to_template
是我在视图中使用的通用视图(而不是在我的网址中),因为像新的 render()
函数,它会自动使用 RequestContext
及其所有 context_processor
但是,由于基于函数的通用视图不推荐,应避免使用 direct_to_template
。或者使用 render
或实际的类,请参阅 https://docs.djangoproject.com/en/1.3/topics/generic-views-migration/
我很高兴我没有在长时间内键入 RequestContext
。
Whats the difference (in language a python/django noob can understand) in a view between render()
, render_to_response()
and direct_to_template()
?
e.g. from Nathan Borror's basic apps examples
def comment_edit(request, object_id, template_name='comments/edit.html'):
comment = get_object_or_404(Comment, pk=object_id, user=request.user)
# ...
return render(request, template_name, {
'form': form,
'comment': comment,
})
But I've also seen
return render_to_response(template_name, my_data_dictionary,
context_instance=RequestContext(request))
And
return direct_to_template(request, template_name, my_data_dictionary)
Whats the difference, what to use in any particular situation?
https://docs.djangoproject.com/en/1.8/topics/http/shortcuts/#render
render(request, template[, dictionary][, context_instance][, content_type][, status][, current_app])
render()
is a brand spanking new shortcut for render_to_response
in 1.3 that will automatically use RequestContext
that I will most definitely be using from now on.
https://docs.djangoproject.com/en/1.8/topics/http/shortcuts/#render-to-response
render_to_response(template[, dictionary][, context_instance][, mimetype])¶
render_to_response
is your standard render function used in the tutorials and such. To use RequestContext
you'd have to specify context_instance=RequestContext(request)
direct_to_template
is a generic view that I use in my views (as opposed to in my urls) because like the new render()
function, it automatically uses RequestContext
and all its context_processor
s.
But direct_to_template
should be avoided as function based generic views are deprecated. Either use render
or an actual class, see https://docs.djangoproject.com/en/1.3/topics/generic-views-migration/
I'm happy I haven't typed RequestContext
in a long, long time.
这篇关于Django - render(),render_to_response()和direct_to_template()之间的区别是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!