SimpleTemplateResponse
This class represents an HTTP response that is rendered from a template and a context dictionary. It defers the rendering process until the response is explicitly requested, allowing for post-rendering callbacks and template manipulation before the final content is generated. The class ensures that content is rendered before it can be accessed, iterated over, or pickled.
Attributes
| Attribute | Type | Description |
|---|---|---|
| rendering_attrs | list = ["template_name", "context_data", "_post_render_callbacks"] | A list of attribute names that should be removed from the object dictionary during pickling to ensure only rendered data is preserved. |
Constructor
Signature
def SimpleTemplateResponse(
template: Any,
context: dict = None,
content_type: str = None,
status: int = None,
charset: str = None,
using: str = None,
headers: dict = None
) - > null
Parameters
| Name | Type | Description |
|---|---|---|
| template | Any | A template object, a path to a template, or a list of paths. |
| context | dict = None | A dictionary of values to be used as the template context. |
| content_type | str = None | The MIME type of the response. |
| status | int = None | The HTTP status code for the response. |
| charset | str = None | The charset to be used for encoding the response. |
| using | str = None | The NAME of a template engine to use for loading the template. |
| headers | dict = None | A dictionary of HTTP headers to add to the response. |
Methods
resolve_template()
@classmethod
def resolve_template(
template: string|list|tuple|object
) - > object
Accept a template object, path-to-template, or list of paths.
Parameters
| Name | Type | Description |
|---|---|---|
| template | `string | list |
Returns
| Type | Description |
|---|---|
object | A template object resolved from the provided input |
resolve_context()
@classmethod
def resolve_context(
context: dict|null
) - > dict|null
Prepares the context data for use in template rendering.
Parameters
| Name | Type | Description |
|---|---|---|
| context | `dict | null` |
Returns
| Type | Description |
|---|---|
| `dict | null` |
rendered_content()
@classmethod
def rendered_content() - > string
Return the freshly rendered content for the template and context described by the TemplateResponse.
Returns
| Type | Description |
|---|---|
string | The rendered string content of the template |
add_post_render_callback()
@classmethod
def add_post_render_callback(
callback: callable
) - > null
Add a new post-rendering callback. If the response has already been rendered, invoke the callback immediately.
Parameters
| Name | Type | Description |
|---|---|---|
| callback | callable | A function to be executed after the response is rendered; it receives the response instance as an argument |
Returns
| Type | Description |
|---|---|
null | null |
render()
@classmethod
def render() - > [SimpleTemplateResponse](simpletemplateresponse.md?sid=django_template_response_simpletemplateresponse)
Render (thereby finalizing) the content of the response. If the content has already been rendered, this is a no-op.
Returns
| Type | Description |
|---|---|
[SimpleTemplateResponse](simpletemplateresponse.md?sid=django_template_response_simpletemplateresponse) | The baked response instance with finalized content |
is_rendered()
@classmethod
def is_rendered() - > boolean
Indicates whether the template and context have been baked into the final response content.
Returns
| Type | Description |
|---|---|
boolean | True if the response has been rendered, False otherwise |
content()
@classmethod
def content() - > bytes
Retrieves the rendered content of the response; raises an error if accessed before rendering.
Returns
| Type | Description |
|---|---|
bytes | The binary rendered content of the response |
content()
@classmethod
def content(
value: string|bytes
) - > null
Set the content for the response.
Parameters
| Name | Type | Description |
|---|---|---|
| value | `string | bytes` |
Returns
| Type | Description |
|---|---|
null | null |