On 12/30/05, Michael Hipp <[EMAIL PROTECTED]> wrote:
> I don't know how to reconcile this with what James Bennett and limodou wrote
> about using render_to_response() instead of HttpResponse() as none of the
> examples in the docs use render_to_response() as their return value from the
> view method. So the advice seems contradictory to the docs.

render_to_response is a shortcut provided to make life easier; notice
that a bit further down in that tutorial, you see this sample view:

def index(request):
    latest_poll_list = polls.get_list(order_by=['-pub_date'], limit=5)
    t = loader.get_template('polls/index')
    c = Context({
        'latest_poll_list': latest_poll_list,
    })
    return HttpResponse(t.render(c))

And then, under the heading "A shortcut: render_to_response()" is this
equivalent:

def index(request):
    latest_poll_list = polls.get_list(order_by=['-pub_date'], limit=5)
    return render_to_response('polls/index', {'latest_poll_list':
latest_poll_list})

Basically, render_to_response is a shortcut which takes the name of a
template and a dictionary, and instantiates a context from the
dictionary, loads the template, renders it and creates an HttpResponse
from the result.

> But I can't see in the tutorial example what is put in the template that will
> receive this "Hello, world..." output.

At that point in the tutorial, you're not actually using a template;
the difference is this:

1. return HttpResponse("Hello, world. You're at the poll index.")

This completely bypasses the template system. It tells Django to send
a response which consists of the string "Hello, world. You're at the
poll index." and nothing else.

2. return HttpResponse(t.render(c))

This does use the template system; it tells Django, "Take the template
loaded in t and render it using the context c, then return the result
as the response."


--
"May the forces of evil become confused on the way to your house."
  -- George Carlin

Reply via email to