~ubuntu-branches/ubuntu/saucy/python-django/saucy-updates

« back to all changes in this revision

Viewing changes to docs/ref/template-response.txt

  • Committer: Package Import Robot
  • Author(s): Luke Faraone, Jakub Wilk, Luke Faraone
  • Date: 2013-05-09 15:10:47 UTC
  • mfrom: (1.1.21) (4.4.27 sid)
  • Revision ID: package-import@ubuntu.com-20130509151047-aqv8d71oj9wvcv8c
Tags: 1.5.1-2
[ Jakub Wilk ]
* Use canonical URIs for Vcs-* fields.

[ Luke Faraone ]
* Upload to unstable.

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
TemplateResponse and SimpleTemplateResponse
3
3
===========================================
4
4
 
5
 
.. versionadded:: 1.3
6
 
 
7
5
.. module:: django.template.response
8
6
   :synopsis: Classes dealing with lazy-rendered HTTP responses.
9
7
 
58
56
Methods
59
57
-------
60
58
 
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)
62
60
 
63
61
    Instantiates a
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.
66
64
 
67
65
    ``template``
68
66
        The full name of a template, or a sequence of template names.
77
75
        The HTTP Status code for the response.
78
76
 
79
77
    ``content_type``
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,
 
78
 
 
79
        .. versionchanged:: 1.5
 
80
 
 
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.
87
88
 
88
89
 
119
120
    rendered :class:`~django.template.response.SimpleTemplateResponse`
120
121
    instance.
121
122
 
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.)
125
126
 
126
 
.. method:: SimpleTemplateResponse.render():
 
127
.. method:: SimpleTemplateResponse.render()
127
128
 
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.
131
132
 
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.
135
135
 
136
136
 
137
137
TemplateResponse objects
147
147
Methods
148
148
-------
149
149
 
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)
151
151
 
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.
169
169
 
170
170
    ``content_type``
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,
 
171
 
 
172
        .. versionchanged:: 1.5
 
173
 
 
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.
178
181
 
179
182
    ``current_app``
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.
192
195
 
193
 
There are three circumstances under which a TemplateResponse will be
 
196
There are three circumstances under which a ``TemplateResponse`` will be
194
197
rendered:
195
198
 
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.
198
201
 
199
202
* When the content of the response is explicitly set by assigning
200
 
  :attr:`response.content`.
 
203
  ``response.content``.
201
204
 
202
205
* After passing through template response middleware, but before
203
206
  passing through response middleware.
204
207
 
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
 
content.
 
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.
209
211
 
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', {})
217
219
    >>> t.render()
218
 
    >>> print t.content
 
220
    >>> print(t.content)
219
221
    Original content
220
222
 
221
223
    # Re-rendering doesn't change content
222
224
    >>> t.template_name = 'new.html'
223
225
    >>> t.render()
224
 
    >>> print t.content
 
226
    >>> print(t.content)
225
227
    Original content
226
228
 
227
229
    # Assigning content does change, no render() call required
228
230
    >>> t.content = t.rendered_content
229
 
    >>> print t.content
 
231
    >>> print(t.content)
230
232
    New content
231
233
 
232
234
Post-render callbacks