Search code examples
django-templatesescapinghtml-escape-charactersdjango-template-filters

Django template excape literal string


Basically, is something like

{{ ">>"|escape }} 

possible?

I can use {{ context_variable|escape}} but can I use a string literal?

I want to avoid littering my template files with >


Solution

  • Yes. Django treats invalid variables as an empty string. That's the case when you pass a string literal. I will have the string literal concatenated to an empty string.

    There is also this answer here. Literal HTML in Django without using a variable?