template-response.txt 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. ===========================================
  2. TemplateResponse and SimpleTemplateResponse
  3. ===========================================
  4. .. module:: django.template.response
  5. :synopsis: Classes dealing with lazy-rendered HTTP responses.
  6. Standard :class:`~django.http.HttpResponse` objects are static structures.
  7. They are provided with a block of pre-rendered content at time of
  8. construction, and while that content can be modified, it isn't in a form that
  9. makes it easy to perform modifications.
  10. However, it can sometimes be beneficial to allow decorators or
  11. middleware to modify a response *after* it has been constructed by the
  12. view. For example, you may want to change the template that is used,
  13. or put additional data into the context.
  14. TemplateResponse provides a way to do just that. Unlike basic
  15. :class:`~django.http.HttpResponse` objects, TemplateResponse objects retain
  16. the details of the template and context that was provided by the view to
  17. compute the response. The final output of the response is not computed until
  18. it is needed, later in the response process.
  19. SimpleTemplateResponse objects
  20. ==============================
  21. .. class:: SimpleTemplateResponse()
  22. Attributes
  23. ----------
  24. .. attribute:: SimpleTemplateResponse.template_name
  25. The name of the template to be rendered. Accepts a backend-dependent
  26. template object (such as those returned by
  27. :func:`~django.template.loader.get_template()`), the name of a template,
  28. or a list of template names.
  29. Example: ``['foo.html', 'path/to/bar.html']``
  30. .. deprecated:: 1.8
  31. ``template_name`` used to accept a :class:`~django.template.Template`.
  32. .. attribute:: SimpleTemplateResponse.context_data
  33. The context data to be used when rendering the template. It must be a
  34. :class:`dict`.
  35. Example: ``{'foo': 123}``
  36. .. deprecated:: 1.8
  37. ``context_data`` used to accept a :class:`~django.template.Context`.
  38. .. attribute:: SimpleTemplateResponse.rendered_content
  39. The current rendered value of the response content, using the current
  40. template and context data.
  41. .. attribute:: SimpleTemplateResponse.is_rendered
  42. A boolean indicating whether the response content has been rendered.
  43. Methods
  44. -------
  45. .. method:: SimpleTemplateResponse.__init__(template, context=None, content_type=None, status=None, charset=None, using=None)
  46. Instantiates a :class:`~django.template.response.SimpleTemplateResponse`
  47. object with the given template, context, content type, HTTP status, and
  48. charset.
  49. ``template``
  50. A backend-dependent template object (such as those returned by
  51. :func:`~django.template.loader.get_template()`), the name of a template,
  52. or a list of template names.
  53. .. deprecated:: 1.8
  54. ``template`` used to accept a :class:`~django.template.Template`.
  55. ``context``
  56. A :class:`dict` of values to add to the template context. By default,
  57. this is an empty dictionary.
  58. .. deprecated:: 1.8
  59. ``context`` used to accept a :class:`~django.template.Context`.
  60. ``content_type``
  61. The value included in the HTTP ``Content-Type`` header, including the
  62. MIME type specification and the character set encoding. If
  63. ``content_type`` is specified, then its value is used. Otherwise,
  64. :setting:`DEFAULT_CONTENT_TYPE` is used.
  65. ``status``
  66. The HTTP status code for the response.
  67. ``charset``
  68. The charset in which the response will be encoded. If not given it will
  69. be extracted from ``content_type``, and if that is unsuccessful, the
  70. :setting:`DEFAULT_CHARSET` setting will be used.
  71. ``using``
  72. The :setting:`NAME <TEMPLATES-NAME>` of a template engine to use for
  73. loading the template.
  74. .. versionchanged:: 1.8
  75. The ``charset`` and ``using`` parameters were added.
  76. .. method:: SimpleTemplateResponse.resolve_context(context)
  77. Preprocesses context data that will be used for rendering a template.
  78. Accepts a :class:`dict` of context data. By default, returns the same
  79. :class:`dict`.
  80. Override this method in order to customize the context.
  81. .. versionchanged:: 1.8
  82. ``resolve_context`` returns a :class:`dict`. It used to return a
  83. :class:`~django.template.Context`.
  84. .. deprecated:: 1.8
  85. ``resolve_context`` no longer accepts a
  86. :class:`~django.template.Context`.
  87. .. method:: SimpleTemplateResponse.resolve_template(template)
  88. Resolves the template instance to use for rendering. Accepts a
  89. backend-dependent template object (such as those returned by
  90. :func:`~django.template.loader.get_template()`), the name of a template,
  91. or a list of template names.
  92. Returns the backend-dependent template object instance to be rendered.
  93. Override this method in order to customize template loading.
  94. .. versionchanged:: 1.8
  95. ``resolve_template`` returns backend-dependent template object. It
  96. used to return a :class:`~django.template.Template`.
  97. .. deprecated:: 1.8
  98. ``resolve_template`` no longer accepts a
  99. :class:`~django.template.Template`.
  100. .. method:: SimpleTemplateResponse.add_post_render_callback()
  101. Add a callback that will be invoked after rendering has taken
  102. place. This hook can be used to defer certain processing
  103. operations (such as caching) until after rendering has occurred.
  104. If the :class:`~django.template.response.SimpleTemplateResponse`
  105. has already been rendered, the callback will be invoked
  106. immediately.
  107. When called, callbacks will be passed a single argument -- the
  108. rendered :class:`~django.template.response.SimpleTemplateResponse`
  109. instance.
  110. If the callback returns a value that is not ``None``, this will be
  111. used as the response instead of the original response object (and
  112. will be passed to the next post rendering callback etc.)
  113. .. method:: SimpleTemplateResponse.render()
  114. Sets ``response.content`` to the result obtained by
  115. :attr:`SimpleTemplateResponse.rendered_content`, runs all post-rendering
  116. callbacks, and returns the resulting response object.
  117. ``render()`` will only have an effect the first time it is called. On
  118. subsequent calls, it will return the result obtained from the first call.
  119. TemplateResponse objects
  120. ========================
  121. .. class:: TemplateResponse()
  122. ``TemplateResponse`` is a subclass of
  123. :class:`~django.template.response.SimpleTemplateResponse` that knows about
  124. the current :class:`~django.http.HttpRequest`.
  125. Methods
  126. -------
  127. .. method:: TemplateResponse.__init__(request, template, context=None, content_type=None, status=None, current_app=None, charset=None, using=None)
  128. Instantiates a :class:`~django.template.response.TemplateResponse` object
  129. with the given request, template, context, content type, HTTP status, and
  130. charset.
  131. ``request``
  132. An :class:`~django.http.HttpRequest` instance.
  133. ``template``
  134. A backend-dependent template object (such as those returned by
  135. :func:`~django.template.loader.get_template()`), the name of a template,
  136. or a list of template names.
  137. .. deprecated:: 1.8
  138. ``template`` used to accept a :class:`~django.template.Template`.
  139. ``context``
  140. A :class:`dict` of values to add to the template context. By default,
  141. this is an empty dictionary.
  142. .. deprecated:: 1.8
  143. ``context`` used to accept a :class:`~django.template.Context`.
  144. ``content_type``
  145. The value included in the HTTP ``Content-Type`` header, including the
  146. MIME type specification and the character set encoding. If
  147. ``content_type`` is specified, then its value is used. Otherwise,
  148. :setting:`DEFAULT_CONTENT_TYPE` is used.
  149. ``status``
  150. The HTTP status code for the response.
  151. ``current_app``
  152. A hint indicating which application contains the current view. See the
  153. :ref:`namespaced URL resolution strategy <topics-http-reversing-url-namespaces>`
  154. for more information.
  155. .. deprecated:: 1.8
  156. The ``current_app`` argument is deprecated. Instead you should set
  157. ``request.current_app``.
  158. ``charset``
  159. The charset in which the response will be encoded. If not given it will
  160. be extracted from ``content_type``, and if that is unsuccessful, the
  161. :setting:`DEFAULT_CHARSET` setting will be used.
  162. ``using``
  163. The :setting:`NAME <TEMPLATES-NAME>` of a template engine to use for
  164. loading the template.
  165. .. versionchanged:: 1.8
  166. The ``charset`` and ``using`` parameters were added.
  167. The rendering process
  168. =====================
  169. Before a :class:`~django.template.response.TemplateResponse` instance can be
  170. returned to the client, it must be rendered. The rendering process takes the
  171. intermediate representation of template and context, and turns it into the
  172. final byte stream that can be served to the client.
  173. There are three circumstances under which a ``TemplateResponse`` will be
  174. rendered:
  175. * When the ``TemplateResponse`` instance is explicitly rendered, using
  176. the :meth:`SimpleTemplateResponse.render()` method.
  177. * When the content of the response is explicitly set by assigning
  178. ``response.content``.
  179. * After passing through template response middleware, but before
  180. passing through response middleware.
  181. A ``TemplateResponse`` can only be rendered once. The first call to
  182. :meth:`SimpleTemplateResponse.render` sets the content of the response;
  183. subsequent rendering calls do not change the response content.
  184. However, when ``response.content`` is explicitly assigned, the
  185. change is always applied. If you want to force the content to be
  186. re-rendered, you can re-evaluate the rendered content, and assign
  187. the content of the response manually::
  188. # Set up a rendered TemplateResponse
  189. >>> from django.template.response import TemplateResponse
  190. >>> t = TemplateResponse(request, 'original.html', {})
  191. >>> t.render()
  192. >>> print(t.content)
  193. Original content
  194. # Re-rendering doesn't change content
  195. >>> t.template_name = 'new.html'
  196. >>> t.render()
  197. >>> print(t.content)
  198. Original content
  199. # Assigning content does change, no render() call required
  200. >>> t.content = t.rendered_content
  201. >>> print(t.content)
  202. New content
  203. Post-render callbacks
  204. ---------------------
  205. Some operations -- such as caching -- cannot be performed on an
  206. unrendered template. They must be performed on a fully complete and
  207. rendered response.
  208. If you're using middleware, the solution is easy. Middleware provides
  209. multiple opportunities to process a response on exit from a view. If
  210. you put behavior in the Response middleware is guaranteed to execute
  211. after template rendering has taken place.
  212. However, if you're using a decorator, the same opportunities do not
  213. exist. Any behavior defined in a decorator is handled immediately.
  214. To compensate for this (and any other analogous use cases),
  215. :class:`TemplateResponse` allows you to register callbacks that will
  216. be invoked when rendering has completed. Using this callback, you can
  217. defer critical processing until a point where you can guarantee that
  218. rendered content will be available.
  219. To define a post-render callback, just define a function that takes
  220. a single argument -- response -- and register that function with
  221. the template response::
  222. from django.template.response import TemplateResponse
  223. def my_render_callback(response):
  224. # Do content-sensitive processing
  225. do_post_processing()
  226. def my_view(request):
  227. # Create a response
  228. response = TemplateResponse(request, 'mytemplate.html', {})
  229. # Register the callback
  230. response.add_post_render_callback(my_render_callback)
  231. # Return the response
  232. return response
  233. ``my_render_callback()`` will be invoked after the ``mytemplate.html``
  234. has been rendered, and will be provided the fully rendered
  235. :class:`TemplateResponse` instance as an argument.
  236. If the template has already been rendered, the callback will be
  237. invoked immediately.
  238. Using TemplateResponse and SimpleTemplateResponse
  239. =================================================
  240. A :class:`TemplateResponse` object can be used anywhere that a normal
  241. :class:`django.http.HttpResponse` can be used. It can also be used as an
  242. alternative to calling :func:`~django.shortcuts.render()` or
  243. :func:`~django.shortcuts.render_to_response()`.
  244. For example, the following simple view returns a :class:`TemplateResponse`
  245. with a simple template and a context containing a queryset::
  246. from django.template.response import TemplateResponse
  247. def blog_index(request):
  248. return TemplateResponse(request, 'entry_list.html', {'entries': Entry.objects.all()})