3

In Django < 1.8 there was possibility to render template without autoescaping:

context = Context({...}, autoescape=False)
result = template.render(context)

In Django 1.8/1.9 I get following deprecation waring:

RemovedInDjango110Warning: render() must be called with a dict, not a Context.

Of course I can change the Context instance to dict:

result = template.render({...})

But how can I force the render function to turn off autoescape without using {% autoescape %} tags in each email template (not every template in my project!)?

Only one solution comes to my mind: iterate over all context (dictionary) items and mark-them-safe (mark_safe), but it doesn't seem elegant.

Alasdair
  • 298,606
  • 55
  • 578
  • 516
EMiDU
  • 654
  • 1
  • 7
  • 24

1 Answers1

3

An autoescape option has been added to the Django template backend in ticket 25469. The change will be included in the upcoming Django 1.10.

Note that you may have to enable two instances of the Django template backend in your TEMPLATES setting, one that enables auto escaping, and one that does not (see this comment from the ticket. You can then choose the correct template backend depending on whether or not you want autoescape to be enabled.

Until Django 1.10 is release, note that passing a Context to render() is only deprecated in Django 1.8. It still works in Django 1.8 and 1.9.

Alasdair
  • 298,606
  • 55
  • 578
  • 516