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

时间:2011-03-01 12:13:51

标签: python django

render()render_to_response()direct_to_template()之间的视图差异(用python / django noob语言可以理解的)?

e.g。来自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)

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

5 个答案:

答案 0 :(得分:182)

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


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 1}} S上。

但应避免使用direct_to_template ,因为不推荐使用基于函数的通用视图。使用render或实际课程,请参阅https://docs.djangoproject.com/en/1.3/topics/generic-views-migration/

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

答案 1 :(得分:38)

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

  • 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

答案 2 :(得分:23)

渲染

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的形式存在视图函数的开销。

答案 3 :(得分:12)

来自django docs

  

render()与调用相同   render_to_response()with a   那个context_instance参数   强制使用RequestContext。

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

答案 4 :(得分:6)

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

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

相关问题