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()?

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,


    return direct_to_template(request, template_name, my_data_dictionary)

Whats the difference, what to use in any particular situation?

5/5/2015 12:26:06 PM

Accepted Answer

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_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)

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

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

9/30/2015 1:02:37 PM

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 without a view defined in but it can be used in 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

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow