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

Rya*_*yan 231 python django

最新的差值(在语言蟒/ django的小白可以理解)在之间的视图render(),render_to_response()direct_to_template()

例如来自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,
    })
Run Code Online (Sandbox Code Playgroud)

但我也看到了

    return render_to_response(template_name, my_data_dictionary,
              context_instance=RequestContext(request))
Run Code Online (Sandbox Code Playgroud)

    return direct_to_template(request, template_name, my_data_dictionary)
Run Code Online (Sandbox Code Playgroud)

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

Yuj*_*ita 184

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

render(request, template[, dictionary][, context_instance][, content_type][, status][, current_app])
Run Code Online (Sandbox Code Playgroud)

render()render_to_response1.3中的一个品牌打屁股新捷径,将自动使用RequestContext我从现在开始肯定会使用的.


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

render_to_response(template[, dictionary][, context_instance][, mimetype])¶
Run Code Online (Sandbox Code Playgroud)

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是我在视图中使用的通用视图(而不是在我的URL中),因为像新render()函数一样,它会自动使用RequestContextcontext_processor的所有内容.

但是direct_to_template 应该避免使用基于函数的通用视图.无论是使用render还是实际的类,请参阅https://docs.djangoproject.com/en/1.3/topics/generic-views-migration/

我很高兴我没有RequestContext长时间打字.

  • 请注意:不推荐使用基于函数的**通用视图**,而不是基于函数的**视图**.Django附带的通用视图现在使用基于类的视图(TemplateView)实现,它们曾经被实现为函数(direct_to_template等).作为功​​能实现的视图,我个人的偏好,仍然受支持,并且不会改变. (6认同)

Rya*_*yan 39

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

  • render_to_response()是"原创",但要求你context_instance=RequestContext(request)几乎所有的时间,一个PITA.

  • direct_to_template()被设计为仅在urls.py中使用而没有在views.py中定义的视图,但它可以在views.py中使用,以避免必须键入RequestContext

  • render()是一个快捷方式render_to_response(),可以自动提供context_instance=Request....它可以在Django开发版本(1.2.1),但许多人都创建了自己的快捷键,比如这一个,这一个或那一个把我最初纳森basic.tools. shortcuts.py

  • 是的,这可能发生在将近 4 年前的链接上! (2认同)

Fro*_*aka 23

渲染是

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

所以render_to_response除了它包装你的上下文使模板预处理器工作之外没有什么区别.

直接到模板是一般视图.

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


Fáb*_*niz 12

来自django docs:

render()与使用context_instance参数调用render_to_response()相同,后者强制使用RequestContext.

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


cli*_*ime 6

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

context_instance = RequestContext(request)
return render_to_response(template_name, user_context, context_instance)
Run Code Online (Sandbox Code Playgroud)

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