Previous Page Next Page

Rendering a Template as an HTTP Response

In the preceding section, you learned how to create a Django template from an HTTP document. This section covers how to display it using the render_to_response() shortcut function. The render_to_response() function accepts the name of a template file and a dictionary as its only two arguments, as shown in the following example:

render_to_response('person_detail.html', {'p': person})

When you call render_to_response(), it opens the template file, reads the contents into a Template object, creates a Context object using the dictionary argument, renders the Template object based on the Context object, and outputs the results as an HttpResponse.

Did you Know?

You can use the locals() Python function to generate a dictionary containing all the locals in the current function context. Instead of building your own dictionary, you can just specify the locals() function in the render_to_response call:

render_to_response('person_detail.html', locals())


Try It Yourself: Render a Web Page from a Template

In this section, you will modify the details() view function for the People application to use render_to_response() to display the Person details using the person_details.html template file you created in the preceding "Try It Yourself" section.

Follow these steps to modify the details() view to use the person_details.html template:

1.
Open the iFriends/People/views.py file in an editor.

2.
Import the render_to_response() function using the following line of code from Listing 7.5:

from django.shortcuts import render_to_response, get_object_or_404

3.
Remove the current contents of the details() function.

4.
Add the following line of code to the details() function to get the Person object specified by the pID argument, as shown in Listing 7.5:

p = get_object_or_404(Person, pk=pID)

5.
Add the following line of code to the details() function to render the view using the person_details.html template, as shown in Listing 7.5:

return render_to_response('people/person_details.html', {'p': p})

6.
Save the views.py file.

7.
Access the following URL in a web browser to verify that the view is working correctly, as shown in Figure 7.6:

http://127.0.0.1:8000/People/Info/2/

Figure 7.6. Web page generated by the details() view of the People application.


Listing 7.5. Full Contents of iFriends/People/views.py

from django.http import HttpResponse
from iFriends.People.models import Person
from django.shortcuts import render_to_response, get_object_or_404

def index(request):
    response = HttpResponse()

    response.write("<h1>People</h1><hr>")
    pList = Person.objects.all()
    for p in pList:
        link = "<a href=\"Info/%d\">" % (p.id)
        response.write("<li>%s%s</a></li>" % (link, p.name))

    return response

def details(request, pID='0', opts=()):
    p = get_object_or_404(Person, pk=pID)
    return render_to_response('people/person_details.html', {'p': p})


Previous Page Next Page