how to filter out the html tags contained in strings in django templates

created at 07-15-2021 views: 1

how to filter out the html tags contained in strings in django templates.

there are two ways:

  • template tag
  • view function

Usage in template

{{ myvar|striptags }}

If the string of myvar is A common way <br> to handle transactions <br> on the web is to wrap <br> each request in a transaction.<br>, then the output result would be: A common way to handle transactions on the web is to wrap each request in a transaction.

Usage in view

If you want to use it in the view function of python django, here is an example:

class AddressForm(forms.ModelForm):

    class Meta:
        model = Address

    def clean(self):
        from django.utils.html import strip_tags
        cleaned_data = super(AddressForm, self).clean()
        cleaned_data['first_name'] = strip_tags(cleaned_data.get('first_name'))
        return cleaned_data
Please log in to leave a comment.