简体   繁体   English

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

[英]Django - what is the difference between render(), render_to_response() and direct_to_template()?

Whats the difference (in language a python/django noob can understand) in a view between render() , render_to_response() and direct_to_template() ?render()render_to_response()direct_to_template()之间的视图中有什么区别(python/django noob 可以理解的语言direct_to_template()

eg from Nathan Borror's basic apps examples例如来自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,
    })

But I've also seen但我也看过

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

And

    return direct_to_template(request, template_name, my_data_dictionary)

Whats the difference, what to use in any particular situation?有什么区别,在任何特定情况下使用什么?

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

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

render() is a brand spanking new shortcut for render_to_response in 1.3 that will automatically use RequestContext that I will most definitely be using from now on. render()是 1.3 中render_to_response全新快捷方式,它将自动使用RequestContext ,从现在开始我肯定会使用它。


2020 EDIT: It should be noted that render_to_response() was removed in Django 3.0 2020 编辑:应该注意的是, render_to_response()在 Django 3.0 中被删除了

https://docs.djangoproject.com/en/1.8/topics/http/shortcuts/#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 is your standard render function used in the tutorials and such. render_to_response是您在教程等中使用的标准渲染函数。 To use RequestContext you'd have to specify context_instance=RequestContext(request)要使用RequestContext您必须指定context_instance=RequestContext(request)


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

direct_to_template is a generic view that I use in my views (as opposed to in my urls) because like the new render() function, it automatically uses RequestContext and all its context_processor s. direct_to_template是我在视图中使用的通用视图(而不是在我的 url 中),因为与新的render()函数一样,它会自动使用RequestContext及其所有context_processor

But direct_to_template should be avoided as function based generic views are deprecated.但是应该避免direct_to_template因为基于函数的通用视图已被弃用。 Either use render or an actual class, see https://docs.djangoproject.com/en/1.3/topics/generic-views-migration/使用render或实际类,请参阅https://docs.djangoproject.com/en/1.3/topics/generic-views-migration/

I'm happy I haven't typed RequestContext in a long, long time.我很高兴我很久没有输入RequestContext了。

Rephrasing Yuri, Fábio, and Frosts answers for the Django noob (ie me) - almost certainly a simplification, but a good starting point?为 Django noob(即我)改写 Yuri、Fábio 和 Frosts 的答案——几乎可以肯定是一种简化,但一个好的起点?

  • render_to_response() is the "original", but requires you putting context_instance=RequestContext(request) in nearly all the time, a PITA. render_to_response()是“原始的”,但要求您几乎一直将context_instance=RequestContext(request)放入 PITA 中。

  • direct_to_template() is designed to be used just in urls.py without a view defined in views.py but it can be used in views.py to avoid having to type RequestContext direct_to_template()旨在仅在 urls.py 中使用,而没有在 views.py 中定义视图,但它可以在 views.py 中使用以避免必须键入 RequestContext

  • render() is a shortcut for render_to_response() that automatically supplies context_instance=Request .... Its available in the django development version (1.2.1) but many have created their own shortcuts such as this one , this one or the one that threw me initially, Nathans basic.tools.shortcuts.py render()render_to_response()的快捷方式,它自动提供context_instance=Request .... 它在 django 开发版本 (1.2.1) 中可用,但许多人已经创建了自己的快捷方式,例如this one , this one or the one最初扔给我,Nathans basic.tools.shortcuts.py

Render is渲染是

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

So there is really no difference between render_to_response except it wraps your context making the template pre-processors work.所以render_to_response之间真的没有区别,除了它包装你的上下文使模板预处理器工作。

Direct to template is a generic view .直接到模板是一个通用视图

There is really no sense in using it here because there is overhead over render_to_response in the form of view function.在这里使用它真的没有意义,因为视图函数形式的render_to_response有开销。

From django docs :来自 Django 文档

render() is the same as a call to render_to_response() with a context_instance argument that that forces the use of a RequestContext. render() 与使用 context_instance 参数调用 render_to_response() 相同,该参数强制使用 RequestContext。

direct_to_template is something different. direct_to_template是不同的东西。 It's a generic view that uses a data dictionary to render the html without the need of the views.py, you use it in urls.py.这是一个通用视图,它使用数据字典来呈现 html,而无需 views.py,您可以在 urls.py 中使用它。 Docs here文档在这里

Just one note I could not find in the answers above.只有一个注释我在上面的答案中找不到。 In this code:在这段代码中:

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

What the third parameter context_instance actually does?第三个参数context_instance实际上是做什么的? Being RequestContext it sets up some basic context which is then added to user_context .作为RequestContext它设置了一些基本的上下文,然后将其添加到user_context So the template gets this extended context.所以模板获得了这个扩展的上下文。 What variables are added is given by TEMPLATE_CONTEXT_PROCESSORS in settings.py.添加的变量由 settings.py 中的TEMPLATE_CONTEXT_PROCESSORS给出。 For instance django.contrib.auth.context_processors.auth adds variable user and variable perm which are then accessible in the template.例如 django.contrib.auth.context_processors.auth 添加变量user和变量perm然后可以在模板中访问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM