【问题标题】:Django - what is the difference between render(), render_to_response() and direct_to_template()?Django - render()、render_to_response() 和 direct_to_template() 有什么区别?
【发布时间】:2011-07-06 11:27:14
【问题描述】:

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

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

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

【问题讨论】:

    标签: python django


    【解决方案1】:

    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 编辑:应该注意render_to_response() 在 Django 3.0 中被删除

    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_processors。

    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 等)。作为函数实现的视图,我个人的偏好,仍然受支持,并且不会改变。
    【解决方案2】:

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

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

    • direct_to_template() 设计为仅在 urls.py 中使用,而没有在 views.py 中定义视图,但它 can be used in views.py to avoid having to type RequestContext

    • render()render_to_response() 的快捷方式,自动提供context_instance=Request.... 它在 django 开发版本 (1.2.1) 中可用,但许多人创建了自己的快捷方式,例如 this onethis one 或最初让我失望的那个,Nathans basic.tools.shortcuts.py

    【讨论】:

    【解决方案3】:

    渲染是

    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 的开销。

    【讨论】:

      【解决方案4】:

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

      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 然后可以在模板中访问。

      【讨论】:

        【解决方案5】:

        来自djangodocs

        render() 与调用相同 render_to_response() 带有 context_instance 参数认为 强制使用 RequestContext。

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-12-31
          • 1970-01-01
          • 2022-11-22
          • 2018-03-12
          • 1970-01-01
          相关资源
          最近更新 更多