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 TemplateIn 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:
Listing 7.5. Full Contents of iFriends/People/views.py
|