render(), render_to_response()和direct_to_template()在视图中有什么区别(python/django新手可以理解的语言)?

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

And

    return direct_to_template(request, template_name, my_data_dictionary)

有什么不同,在特定情况下用什么?


当前回答

换句话说,Yuri, Fábio和Frosts是Django新手(即我)的答案——几乎可以肯定是一个简化,但一个好的起点?

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

其他回答

来自django docs:

Render()与调用 Render_to_response () Context_instance参数 强制使用RequestContext。

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

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是我在我的视图中使用的通用视图(而不是在我的url中),因为像新的render()函数一样,它自动使用RequestContext及其所有的context_processors。

但是应该避免使用direct_to_template,因为不赞成使用基于函数的泛型视图。要么使用渲染,要么使用实际的类,参见https://docs.djangoproject.com/en/1.3/topics/generic-views-migration/

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

渲染是

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

换句话说,Yuri, Fábio和Frosts是Django新手(即我)的答案——几乎可以肯定是一个简化,但一个好的起点?

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

只有一点我在上面的答案中找不到。在这段代码中:

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