How can I tell Django templates not to parse a block containing code that looks like template tags?

Jake picture Jake · Nov 17, 2011 · Viewed 7.3k times · Source

I've got some html files that include templates to be used by jQuery.tmpl. Some tmpl tags (like {{if...}}) look like Django template tags and cause a TemplateSyntaxError. Is there a way I can specify the Django template system should ignore a few lines and output them exactly as they are?

Answer

Michael C. O'Connor picture Michael C. O'Connor · Nov 17, 2011

As of Django 1.5, this is now handled by the built-in verbatim template tag.

In older versions of Django, the built-in way would be to manually escape each template item with the templatetag template tag ( https://docs.djangoproject.com/en/1.3/ref/templates/builtins/#templatetag ), but I suspect that that's not what you want to do.

What you really want is a way to mark a whole block as raw (rather than interpretable) text, which requires a new custom tag. You might want to check out the raw tag here: http://www.holovaty.com/writing/django-two-phased-rendering/