Skip to main content

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

AttributeTypeDescription
rendering_attrslist = ["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

NameTypeDescription
templateAnyA template object, a path to a template, or a list of paths.
contextdict = NoneA dictionary of values to be used as the template context.
content_typestr = NoneThe MIME type of the response.
statusint = NoneThe HTTP status code for the response.
charsetstr = NoneThe charset to be used for encoding the response.
usingstr = NoneThe NAME of a template engine to use for loading the template.
headersdict = NoneA 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

NameTypeDescription
template`stringlist

Returns

TypeDescription
objectA 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

NameTypeDescription
context`dictnull`

Returns

TypeDescription
`dictnull`

rendered_content()

@classmethod
def rendered_content() - > string

Return the freshly rendered content for the template and context described by the TemplateResponse.

Returns

TypeDescription
stringThe 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

NameTypeDescription
callbackcallableA function to be executed after the response is rendered; it receives the response instance as an argument

Returns

TypeDescription
nullnull

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

TypeDescription
[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

TypeDescription
booleanTrue 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

TypeDescription
bytesThe binary rendered content of the response

content()

@classmethod
def content(
value: string|bytes
) - > null

Set the content for the response.

Parameters

NameTypeDescription
value`stringbytes`

Returns

TypeDescription
nullnull