Home > django > [django] create a formatted string with the Django template system

[django] create a formatted string with the Django template system

Problem
In an application I wanted to send formatted HTML emails. For producing the body of the email, I wanted to use the Django template system. The task is very similar to creating a view. The difference is that I don’t want to send back the HTML to the client in the browser but I want to store it in a string.

Solution
The solution is very simple:

from django.template.loader import render_to_string

def email():
    context = {'some_key': 'some_value'}
    html = render_to_string('APP_NAME/email.html', context)
    send_email(html)

This tip is from here.

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: