Skip to main content

BaseRenderer

This class serves as an abstract base for template rendering, providing a consistent interface for generating HTML output for forms, formsets, and fields. It defines default template paths and a core render method while requiring subclasses to implement specific template loading logic via the get_template method.

Attributes

AttributeTypeDescription
form_template_namestring = django/forms/div.htmlThe default path to the template used for rendering a form.
formset_template_namestring = django/forms/formsets/div.htmlThe default path to the template used for rendering a formset.
field_template_namestring = django/forms/field.htmlThe default path to the template used for rendering a single form field.
bound_field_classclassThe class used to represent a field that has been connected to a data source.

Methods


get_template()

@classmethod
def get_template(
template_name: string
) - > [Template](../../template/base/template.md?sid=django_template_base_template)

Retrieves a template object by its name. Subclasses must implement this method to define how templates are loaded from the filesystem or other storage.

Parameters

NameTypeDescription
template_namestringThe path or identifier of the template to be retrieved

Returns

TypeDescription
[Template](../../template/base/template.md?sid=django_template_base_template)The template instance corresponding to the provided template name

render()

@classmethod
def render(
template_name: string,
context: dict,
request: [HttpRequest](../../http/request/httprequest.md?sid=django_http_request_httprequest) = null
) - > string

Renders a template with a given context and optional request object. This method fetches the template via get_template and returns the resulting string with leading and trailing whitespace removed.

Parameters

NameTypeDescription
template_namestringThe path or identifier of the template to render
contextdictA dictionary of data used to populate the template variables
request[HttpRequest](../../http/request/httprequest.md?sid=django_http_request_httprequest) = nullAn optional HTTP request object used for context processors and CSRF token generation

Returns

TypeDescription
stringThe rendered HTML content as a stripped string