Ticket #14855: template-respose-rest.diff
File template-respose-rest.diff, 9.2 KB (added by , 14 years ago) |
---|
-
docs/ref/template-response.txt
diff --git a/docs/ref/template-response.txt b/docs/ref/template-response.txt index 09bcd16..4332e6e 100644
a b TemplateResponse and SimpleTemplateResponse 7 7 .. module:: django.template.response 8 8 :synopsis: Classes dealing with lazy-rendered HTTP responses. 9 9 10 Standard HttpResponse objects are static structures. They are provided11 with a block of pre-rendered content at time of construction, and 12 while that content can be modified, it isn't in a form that makes it13 easy to perform modifications.10 Standard :class:`~django.http.HttpResponse` objects are static structures. 11 They are provided with a block of pre-rendered content at time of 12 construction, and while that content can be modified, it isn't in a form that 13 makes it easy to perform modifications. 14 14 15 15 However, it can sometimes be beneficial to allow decorators or 16 16 middleware to modify a response *after* it has been constructed by the … … view. For example, you may want to change the template that is used, 18 18 or put additional data into the context. 19 19 20 20 TemplateResponse provides a way to do just that. Unlike basic 21 HttpResponse objects, TemplateResponse objects retain the details of 22 the template and context that was provided by the view to compute the23 response. The final output of the response is not computed until21 :class:`~django.http.HttpResponse` objects, TemplateResponse objects retain 22 the details of the template and context that was provided by the view to 23 compute the response. The final output of the response is not computed until 24 24 it is needed, later in the response process. 25 25 26 TemplateResponse objects27 ======================== 26 SimpleTemplateResponse objects 27 ============================== 28 28 29 29 .. class:: SimpleTemplateResponse() 30 30 … … Attributes 33 33 34 34 .. attribute:: SimpleTemplateResponse.template_name 35 35 36 The name of the template to be rendered. Accepts 37 :class:` django.template.Template` object, path totemplate or list38 of paths.36 The name of the template to be rendered. Accepts a 37 :class:`~django.template.Template` object, a path to a template or list 38 of template paths. 39 39 40 40 Example: ``['foo.html', 'path/to/bar.html']`` 41 41 … … Attributes 46 46 47 47 Example: ``{'foo': 123}`` 48 48 49 .. attr :: SimpleTemplateResponse.rendered_content:49 .. attribute:: SimpleTemplateResponse.rendered_content 50 50 51 51 The current rendered value of the response content, using the current 52 52 template and context data. 53 53 54 .. attr :: SimpleTemplateResponse.is_rendered:54 .. attribute:: SimpleTemplateResponse.is_rendered 55 55 56 56 A boolean indicating whether the response content has been rendered. 57 57 … … Methods 61 61 62 62 .. method:: SimpleTemplateResponse.__init__(template, context=None, mimetype=None, status=None, content_type=None) 63 63 64 Instantiates a n64 Instantiates a 65 65 :class:`~django.template.response.SimpleTemplateResponse` object 66 66 with the given template, context, MIME type and HTTP status. 67 67 68 ``template`` is a full name of a template, or a sequence of69 template names. :class:`django.template.Template` instances can70 also be used.68 ``template`` 69 The full name of a template, or a sequence of template names. 70 :class:`~django.template.Template` instances can also be used. 71 71 72 ``context`` is a dictionary of values to add to the template73 context. By default, this is an empty dictionary.74 :class:`~django.template.Context` objects are also accepted as75 ``context`` values.72 ``context`` 73 A dictionary of values to add to the template context. By default, 74 this is an empty dictionary. :class:`~django.template.Context` objects 75 are also accepted as ``context`` values. 76 76 77 ``status`` is the HTTP Status code for the response. 77 ``status`` 78 The HTTP Status code for the response. 78 79 79 ``content_type`` is an alias for ``mimetype``. Historically, this80 parameter was only called ``mimetype``, but since this is actually81 the value included in the HTTP ``Content-Type`` header, it can82 also include the character set encoding, which makes it more than83 just a MIME type specification. If ``mimetype`` is specified (not84 ``None``), that value is used. Otherwise, ``content_type`` is85 used. If neither is given, the ``DEFAULT_CONTENT_TYPE`` setting is86 used.80 ``content_type`` 81 An alias for ``mimetype``. Historically, this parameter was only called 82 ``mimetype``, but since this is actually the value included in the HTTP 83 ``Content-Type`` header, it can also include the character set encoding, 84 which makes it more than just a MIME type specification. If ``mimetype`` 85 is specified (not ``None``), that value is used. Otherwise, 86 ``content_type`` is used. If neither is given, 87 :setting:`DEFAULT_CONTENT_TYPE` is used. 87 88 88 89 89 90 .. method:: SimpleTemplateResponse.resolve_context(context) … … Methods 115 116 the result obtained from the first call. 116 117 117 118 119 TemplateResponse objects 120 ======================== 121 118 122 .. class:: TemplateResponse() 119 123 120 TemplateResponse is a subclass of :class:`SimpleTemplateResponse 121 <django.template.response.SimpleTemplateResponse>` that uses 122 RequestContext instead of Context. 124 TemplateResponse is a subclass of 125 :class:`~django.template.response.SimpleTemplateResponse` that uses 126 a :class:`~django.template.RequestContext` instead of 127 a :class:`~django.template.Context`. 128 129 Methods 130 ------- 123 131 124 132 .. method:: TemplateResponse.__init__(request, template, context=None, mimetype=None, status=None, content_type=None) 125 133 126 Instantiates an ``TemplateResponse`` object with the given127 template, context, MIME type and HTTP status.134 Instantiates an ``TemplateResponse`` object with the given 135 template, context, MIME type and HTTP status. 128 136 129 ``request`` is a HttpRequest instance. 137 ``request`` 138 An :class:`~django.http.HttpRequest` instance. 130 139 131 ``template`` is a full name of a template to use or sequence of132 template names. :class:`django.template.Template` instances are133 also accepted.140 ``template`` 141 The full name of a template, or a sequence of template names. 142 :class:`~django.template.Template` instances can also be used. 134 143 135 ``context`` is a dictionary of values to add to the template 136 context. By default, this is an empty dictionary; context objects 137 are also accepted as ``context`` values. 144 ``context`` 145 A dictionary of values to add to the template context. By default, 146 this is an empty dictionary. :class:`~django.template.Context` objects 147 are also accepted as ``context`` values. 138 148 139 ``status`` is the HTTP Status code for the response. 149 ``status`` 150 The HTTP Status code for the response. 140 151 141 ``content_type`` is an alias for ``mimetype``. Historically, this 142 parameter was only called ``mimetype``, but since this is actually 143 the value included in the HTTP ``Content-Type`` header, it can also 144 include the character set encoding, which makes it more than just a 145 MIME type specification. If ``mimetype`` is specified (not 146 ``None``), that value is used. Otherwise, ``content_type`` is used. 147 If neither is given, the ``DEFAULT_CONTENT_TYPE`` setting is used. 152 ``content_type`` 153 An alias for ``mimetype``. Historically, this parameter was only called 154 ``mimetype``, but since this is actually the value included in the HTTP 155 ``Content-Type`` header, it can also include the character set encoding, 156 which makes it more than just a MIME type specification. If ``mimetype`` 157 is specified (not ``None``), that value is used. Otherwise, 158 ``content_type`` is used. If neither is given, 159 :setting:`DEFAULT_CONTENT_TYPE` is used. 148 160 149 161 150 162 The rendering process 151 163 ===================== 152 164 153 Before a :class:` TemplateResponse()` instance can be returned to the154 client, it must be rendered. The rendering process takes the155 intermediate representation of template and context, and turns it into 156 thefinal byte stream that can be served to the client.165 Before a :class:`~django.template.response.TemplateResponse` instance can be 166 returned to the client, it must be rendered. The rendering process takes the 167 intermediate representation of template and context, and turns it into the 168 final byte stream that can be served to the client. 157 169 158 170 There are three circumstances under which a TemplateResponse will be 159 171 rendered: … … rendered: 168 180 passing through response middleware. 169 181 170 182 A TemplateResponse can only be rendered once. The first call to 171 :meth:`SimpleTemplateResponse.render ()` sets the content of the183 :meth:`SimpleTemplateResponse.render` sets the content of the 172 184 response; subsequent rendering calls do not change the response 173 185 content. 174 186 … … Using TemplateResponse and SimpleTemplateResponse 199 211 200 212 A TemplateResponse object can be used anywhere that a normal 201 213 HttpResponse can be used. It can also be used as an alternative to 202 calling :meth od:`~django.shortcuts.render_to_response()`.214 calling :meth:`~django.shortcuts.render_to_response()`. 203 215 204 216 For example, the following simple view returns a 205 217 :class:`TemplateResponse()` with a simple template, and a context