mgbhargava
mgbhargava

Reputation: 11

how to remove html tags in django template on browser while showing to user

As shown in figure i used {{options|safe}} for rendering options in my django 3.0 polls application even though it is rendering like that and i don't know how to remove the

tags from rendered string, thanks for help in advance regarding

tag error

Upvotes: 0

Views: 1554

Answers (1)

Emin Mastizada
Emin Mastizada

Reputation: 1405

To remove tags, I would recommend using Mozilla's bleach library.

In order to remove tags only in the front-end, not the data itself, you can easily create a custom template filter and clean the tags inside it.

Another cool idea would be to have list of enabled HTML tags that can be used (like making text bold with <b>...</b>) and then render the input as a valid html:

{{ options|remove_tags|safe }}

Example for a custom template filter:

@register.filter
def remove_tags(value):
    return bleach.clean(value, tags=["b", "i"])

Upvotes: 1

Related Questions