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

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

最新的差值(在语言蟒/ 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,
    })

但我也看到了

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

    return direct_to_template(request, template_name, my_data_dictionary)

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

Whats the difference (in language a python/django noob can understand) in a view between render(), render_to_response() and direct_to_template()?

e.g. from 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,
    })

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?


回答 0

https://docs.djangoproject.com/zh-CN/1.8/topics/http/shortcuts/#render

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

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


2020年编辑:应该指出的是render_to_response(),在Django 3.0中已将其删除

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

direct_to_template是我在视图中使用的通用视图(而不是在URL中使用),因为像新render()功能一样,它会自动使用RequestContext及其所有context_processors。

但是direct_to_template 应避免使用,因为不建议使用基于函数的通用视图。使用render还是实际使用的类,请参见https://docs.djangoproject.com/en/1.3/topics/generic-views-migration/

很高兴我很久没输入RequestContext了。

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.


2020 EDIT: It should be noted that render_to_response() was removed in 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 is your standard render function used in the tutorials and such. To use RequestContext you’d have to specify context_instance=RequestContext(request)


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

But direct_to_template should be avoided as function based generic views are deprecated. Either use render or an actual class, see https://docs.djangoproject.com/en/1.3/topics/generic-views-migration/

I’m happy I haven’t typed RequestContext in a long, long time.


回答 1

重新定义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)中可用,但许多人创建了自己的快捷方式,例如this这个或最初让我扔的快捷方式,即Nathans basic.tools。快捷方式

Rephrasing Yuri, Fábio, and Frosts answers for the Django noob (i.e. me) – almost certainly a simplification, but a good starting point?

  • render_to_response() is the “original”, but requires you putting context_instance=RequestContext(request) in nearly all the time, a 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

  • 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


回答 2

渲染为

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只是它包装了使模板预处理器正常工作的上下文。

直接到模板是通用视图

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

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.

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.


回答 3

从django docs

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

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

From django docs:

render() is the same as a call to render_to_response() with a context_instance argument that that forces the use of a RequestContext.

direct_to_template is something different. 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. Docs here


回答 4

我在上面的答案中找不到一个便笺。在此代码中:

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

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

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? Being RequestContext it sets up some basic context which is then added to user_context. So the template gets this extended context. What variables are added is given by TEMPLATE_CONTEXT_PROCESSORS in settings.py. For instance django.contrib.auth.context_processors.auth adds variable user and variable perm which are then accessible in the template.