5.6.2 Shortening Code with render_to_response()
Most views must do the following:
1. Load a template file as a Template
object.
2. Create a Context
from a dictionary.
3. Render the Template
with the Context
.
4. Instantiate an HttpResponse
object with the rendered result.
Django supplies not one but two shortcuts to perform this process for us. The first is the render_to_response()
shortcut. The shortcut replaces the behavior that we currently have in our views, performing all four tasks listed above. Let’s start by importing it, adding it to the end of our pre-existing shortcut import, as shown in Example 5.21.
organizer/views.py
in 5ff3dee4fa
1 from django.shortcuts import ( 2 get_object_or_404, ...
Get Django Unleashed now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.