ChatGPT解决这个技术问题 Extra ChatGPT

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

render()render_to_response()direct_to_template() 之间的视图有什么区别(python/django noob 可以理解的语言)?

例如来自 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,
    })

但我也见过

    return render_to_response(template_name, my_data_dictionary,
              context_instance=RequestContext(request))

    return direct_to_template(request, template_name, my_data_dictionary)

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


t
tfantina

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

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

render() 是 1.3 中 render_to_response 的全新快捷方式,它将自动使用 RequestContext,我从现在开始肯定会使用它。

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

https://docs.djangoproject.com/en/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/en/1.8/ref/generic-views/#django-views-generic-simple-direct-to-template

direct_to_template 是我在视图中(而不是在我的网址中)使用的通用视图,因为与新的 render() 函数一样,它会自动使用 RequestContext 及其所有 context_processor

但是direct_to_template应该避免,因为基于函数的通用视图已被弃用。使用 render 或实际类,请参阅 https://docs.djangoproject.com/en/1.3/topics/generic-views-migration/

我很高兴很长时间没有输入 RequestContext


更正。根据文档,render() 从 1.3 开始可用。
@AppleGrew,不错的收获! “社区”修改了我的帖子以指向特定的分支,他们选择了 1.4
请注意:不推荐使用基于功能的通用视图,而不是基于功能的视图。 Django 附带的通用视图现在使用基于类的视图(TemplateView)来实现,它们曾经被实现为函数(direct_to_template 等)。作为函数实现的视图,我个人的偏好,仍然受支持并且不会改变。
L
Lucio

为 Django 菜鸟(即我)改写 Yuri、Fábio 和 Frosts 的答案——几乎可以肯定是一个简化,但一个好的起点?

render_to_response() 是“原始”,但要求您几乎始终将 context_instance=RequestContext(request) 放入 PITA 中。

direct_to_template() 旨在仅在 urls.py 中使用,而无需在 views.py 中定义视图,但它可以在 views.py 中使用以避免键入 RequestContext

render() 是自动提供 context_instance=Request.. 的 render_to_response() 的快捷方式。它在 django 开发版本 (1.2.1) 中可用,但许多人已经创建了自己的快捷方式,例如这个、这个或那个最初扔给我,Nathans basic.tools.shortcuts.py


第一个链接 (import-awesome.com/…) 给出 404
是的,这可能发生在将近 4 年的链接上!
o
oz123

渲染是

def render(request, *args, **kwargs):
    """ Simple wrapper for render_to_response. """
    kwargs['context_instance'] = RequestContext(request)
    return render_to_response(*args, **kwargs)

因此,render_to_response 之间实际上没有区别,只是它包装了您的上下文,使模板预处理器工作。

直接到模板是 generic view

在这里使用它确实没有任何意义,因为在视图函数的形式中存在超过 render_to_response 的开销。


W
Wladimir Palant

来自 django docs

render() 与使用强制使用 RequestContext 的 context_instance 参数调用 render_to_response() 相同。

direct_to_template 有所不同。这是一个通用视图,它使用数据字典来呈现 html 而不需要 views.py,您可以在 urls.py 中使用它。文档here


c
clime

只有一个注释我在上面的答案中找不到。在这段代码中:

context_instance = RequestContext(request)
return render_to_response(template_name, user_context, context_instance)

第三个参数 context_instance 实际上做了什么?作为 RequestContext,它设置了一些基本上下文,然后将其添加到 user_context。所以模板得到了这个扩展的上下文。添加的变量由 settings.py 中的 TEMPLATE_CONTEXT_PROCESSORS 给出。例如 django.contrib.auth.context_processors.auth 添加变量 user 和变量 perm,然后可以在模板中访问它们。