In the docs it says:
The only exceptions are variables that are already marked as “safe” from escaping, either by the code that populated the variable, or because it has had the safe or escape filters applied.”
How does the “populated the variable” part work ? I’m actually looking for a way to declare a template tag as safe in the view. I somehow think it’s not a good idea to let a designer decide. My co-worker will just add it whenever she ‘thinks’ it’s a good idea.
https://docs.djangoproject.com/en/dev/ref/templates/builtins/?from=olddocs
Django has a subclass of strings called safe strings (specifically
SafeUnicodeorSafeString), which can be created usingdjango.utils.safestring.mark_safe. When the template engine comes across a safe string it doesn’t perform HTML escaping on it:If you’re writing your own template tag, you need to implement
render()which will return a string that will be treated as safe, meaning you have to handle any escaping necessary yourself. However if you’re writing a template filter, you can set the attributeis_safe = Trueon the filter to avoid auto escaping of the returned value, e.g.See https://docs.djangoproject.com/en/4.0/howto/custom-template-tags/#filters-and-auto-escaping for more details.