61
.. method:: SimpleTemplateResponse.__init__(template, context=None, mimetype=None, status=None, content_type=None)
59
.. method:: SimpleTemplateResponse.__init__(template, context=None, content_type=None, status=None)
64
62
:class:`~django.template.response.SimpleTemplateResponse` object
65
with the given template, context, MIME type and HTTP status.
63
with the given template, context, content type, and HTTP status.
68
66
The full name of a template, or a sequence of template names.
77
75
The HTTP Status code for the response.
80
An alias for ``mimetype``. Historically, this parameter was only called
81
``mimetype``, but since this is actually the value included in the HTTP
82
``Content-Type`` header, it can also include the character set encoding,
83
which makes it more than just a MIME type specification. If ``mimetype``
84
is specified (not ``None``), that value is used. Otherwise,
85
``content_type`` is used. If neither is given,
79
.. versionchanged:: 1.5
81
Historically, this parameter was only called ``mimetype`` (now
82
deprecated), but since this is actually the value included in the HTTP
83
``Content-Type`` header, it can also include the character set
84
encoding, which makes it more than just a MIME type specification. If
85
``mimetype`` is specified (not ``None``), that value is used.
86
Otherwise, ``content_type`` is used. If neither is given,
86
87
:setting:`DEFAULT_CONTENT_TYPE` is used.
119
120
rendered :class:`~django.template.response.SimpleTemplateResponse`
122
If the callback returns a value that is not `None`, this will be
123
If the callback returns a value that is not ``None``, this will be
123
124
used as the response instead of the original response object (and
124
125
will be passed to the next post rendering callback etc.)
126
.. method:: SimpleTemplateResponse.render():
127
.. method:: SimpleTemplateResponse.render()
128
Sets :attr:`response.content` to the result obtained by
129
Sets ``response.content`` to the result obtained by
129
130
:attr:`SimpleTemplateResponse.rendered_content`, runs all post-rendering
130
131
callbacks, and returns the resulting response object.
132
:meth:`~SimpleTemplateResponse.render()` will only have an effect
133
the first time it is called. On subsequent calls, it will return
134
the result obtained from the first call.
133
``render()`` will only have an effect the first time it is called. On
134
subsequent calls, it will return the result obtained from the first call.
137
137
TemplateResponse objects
150
.. method:: TemplateResponse.__init__(request, template, context=None, mimetype=None, status=None, content_type=None, current_app=None)
150
.. method:: TemplateResponse.__init__(request, template, context=None, content_type=None, status=None, current_app=None)
152
152
Instantiates an ``TemplateResponse`` object with the given
153
153
template, context, MIME type and HTTP status.
168
168
The HTTP Status code for the response.
171
An alias for ``mimetype``. Historically, this parameter was only called
172
``mimetype``, but since this is actually the value included in the HTTP
173
``Content-Type`` header, it can also include the character set encoding,
174
which makes it more than just a MIME type specification. If ``mimetype``
175
is specified (not ``None``), that value is used. Otherwise,
176
``content_type`` is used. If neither is given,
172
.. versionchanged:: 1.5
174
Historically, this parameter was only called ``mimetype`` (now
175
deprecated), but since this is actually the value included in the HTTP
176
``Content-Type`` header, it can also include the character set
177
encoding, which makes it more than just a MIME type specification. If
178
``mimetype`` is specified (not ``None``), that value is used.
179
Otherwise, ``content_type`` is used. If neither is given,
177
180
:setting:`DEFAULT_CONTENT_TYPE` is used.
190
193
intermediate representation of template and context, and turns it into the
191
194
final byte stream that can be served to the client.
193
There are three circumstances under which a TemplateResponse will be
196
There are three circumstances under which a ``TemplateResponse`` will be
196
* When the TemplateResponse instance is explicitly rendered, using
199
* When the ``TemplateResponse`` instance is explicitly rendered, using
197
200
the :meth:`SimpleTemplateResponse.render()` method.
199
202
* When the content of the response is explicitly set by assigning
200
:attr:`response.content`.
203
``response.content``.
202
205
* After passing through template response middleware, but before
203
206
passing through response middleware.
205
A TemplateResponse can only be rendered once. The first call to
206
:meth:`SimpleTemplateResponse.render` sets the content of the
207
response; subsequent rendering calls do not change the response
208
A ``TemplateResponse`` can only be rendered once. The first call to
209
:meth:`SimpleTemplateResponse.render` sets the content of the response;
210
subsequent rendering calls do not change the response content.
210
However, when :attr:`response.content` is explicitly assigned, the
212
However, when ``response.content`` is explicitly assigned, the
211
213
change is always applied. If you want to force the content to be
212
214
re-rendered, you can re-evaluate the rendered content, and assign
213
215
the content of the response manually::
215
217
# Set up a rendered TemplateResponse
216
218
>>> t = TemplateResponse(request, 'original.html', {})
221
223
# Re-rendering doesn't change content
222
224
>>> t.template_name = 'new.html'
227
229
# Assigning content does change, no render() call required
228
230
>>> t.content = t.rendered_content
232
234
Post-render callbacks