您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

Django-render(),render_to_response()和direct_to_template()有什么区别?

Django-render(),render_to_response()和direct_to_template()有什么区别?

https://docs.djangoproject.com/zh- CN/1.8/topics/http/shortcuts/#render

render(request, template[, dictionary][, context_instance][, content_type][, status][, current_app])

render()一个render_to_response在1.3中崭新的快捷方式的品牌,该快捷方式将自动使用RequestContext,从现在开始我肯定会使用它。

2020年编辑:应该指出的是render_to_response(),在Django 3.0中已将其删除

https://docs.djangoproject.com/zh-CN/1.8/topics/http/shortcuts/#render-to- response

render_to_response(template[, dictionary][, context_instance][, mimetype])¶

render_to_response是教程等中使用的标准渲染功能。要使用,RequestContext您必须指定context_instance=RequestContext(request)

https://docs.djangoproject.com/zh-CN/1.8/ref/generic-views/#django-views- generic-simple-direct-to- template

direct_to_template是我在视图中使用的通用视图(而不是在URL中使用),因为像新render()功能一样,它会自动使用RequestContext及其所有context_processors。

但是direct_to_template 应避免使用, 因为不建议使用基于函数的通用视图。使用render还是实际使用的类,请参见https://docs.djangoproject.com/en/1.3/topics/generic- views-migration/

我很高兴很久没打打字RequestContext了。

Go 2022/1/1 18:42:46 有354人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶