[Fixed]-Django filters to return HTML that will be rendered in the template

28👍

To disable autoescape you can use mark_safe method:

from django.utils.safestring import mark_safe

@register.filter(is_safe=True)
def format_description(description):
    text = ''
    for i in description.split('\n'):
        text += ('<p class="site-description">' + i + '</p>')
    return mark_safe(text)

10👍

This is explicitly covered in the documentation: Filters and auto-escaping.

You need to mark the output as safe.

from django.utils.safestring import mark_safe
...
return mark_safe(text)

Leave a comment