Django-render()、render_to_response()和direct_to_template()之间有什么区别?

render()render\u-to\u-response()direct\u-to\u-template()之间的视图有什么区别(用python/django-noob可以理解的语言)

e、 g.来自Nathan Borror的基本应用程序示例

def comment\u edit(请求、对象id、模板名称='comments/edit.html'):
comment=get\u object\u或\u 404(comment,pk=object\u id,user=request.user)
# ...
返回渲染(请求、模板名称、{
“形式”:形式,
“评论”:评论,
})

但我也看到了

返回渲染到响应(模板名称、我的数据字典、,
上下文\实例=请求上下文(请求))

直接返回模板(请求、模板名称、我的数据字典)

有什么区别,在特定情况下使用什么

https://docs.djangoproject.com/en/1.8/topics/http/shortcuts/#render

呈现(请求、模板[、字典][、上下文实例][、内容类型][、状态][、当前应用程序])

render()是1.3中的render\u to\u response的一个全新快捷方式,它将自动使用RequestContext,从现在起我肯定会使用它


2020年编辑:应该注意的是,Django 3.0中删除了render_to_response()

https://docs.djangoproject.com/en/1.8/topics/http/shortcuts/#render-回应

呈现到响应(模板[,字典][,上下文实例][,mimetype])中

render_to_response是教程等中使用的标准渲染函数。要使用RequestContext必须指定context\u instance=RequestContext(request)


https://docs.djangoproject.com/en/1.8/ref/generic-views/#django-视图通用简单直接到模板

direct\u to\u template是我在视图中使用的一个通用视图(与我的URL相反),因为与新的render()函数一样,它自动使用RequestContext及其所有上下文处理器

但是应该避免使用直接到<u模板,因为不推荐使用基于函数的通用视图。使用render或实际类,请参见https://docs.djangoproject.com/en/1.3/topics/generic-views-migration/

我很高兴我已经很久很久没有键入RequestContext

发表评论