Utility Functions

render_email_template(template_name, context=None, request=None)

Render an email template stored in the database using Django’s template engine.

This function looks up a Template object by its name field, compiles its content into a Django Template, and renders it with the provided context. It is designed to generate dynamic email content.

Parameters:
  • template_name (str) –

    The name of the template to retrieve from the database.

  • context (dict, default: None ) –

    A dictionary of context variables to pass to the template. Defaults to an empty dictionary.

  • request (HttpRequest, default: None ) –

    An optional Django request object, which will be included in the template context under the key 'request'.

Returns:
  • email_content( str ) –

    The rendered email content as a string.

Raises:
Source code in easy_email/utils.py
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
def render_email_template(template_name, context=None, request=None):
    """
    Render an email template stored in the database using Django's template engine.

    This function looks up a `Template` object by its `name` field, compiles its
    `content` into a Django `Template`, and renders it with the provided context.
    It is designed to generate dynamic email content.

    Args:
        template_name (str): The name of the template to retrieve from the database.
        context (dict, optional): A dictionary of context variables to pass to the
            template. Defaults to an empty dictionary.
        request (HttpRequest, optional): An optional Django request object, which
            will be included in the template context under the key `'request'`.

    Returns:
        email_content (str): The rendered email content as a string.

    Raises:
        TemplateNotFound: If no template with the given `template_name` exists.
    """
    try:
        template = Template.objects.get(name=template_name)
    except:
        raise TemplateNotFound(template_name)

    template_engine = TemplateEngine(template_string=template.content)
    context = Context({'request': request, **context })
    email_content = template_engine.render(context)
    return email_content