Django Template.render Causing Escaped Characters Converted into HTML Entities

Recently I encountered this issue and thought I should share the resolution for any one who might face this too.

The problem was that I was using Django templating inside my .py script. The value being passed to be processed by Template.render had already escaped double quotes like:

some_variable = "{\"custom_field\":\"custom_value\"}"

When being passed through the Template.render method, following was what I was getting back:

output_variable = "{"custom_field":"custom_value"}"

The solution was to instruct the passing Context not to auto escape. So for example following code worked for me:

template = Template(template)
context = Context(input_data, autoescape=False)
data = template.render(context)

Leave a Reply

Your email address will not be published. Required fields are marked *