Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to strip html/javascript from text input in django

Tags:

python

django

What is the easiest way to strip all html/javascript from a string?

like image 266
Eldila Avatar asked May 18 '09 22:05

Eldila


People also ask

What does it mean to strip HTML?

stripHtml( html ) Changes the provided HTML string into a plain text string by converting <br> , <p> , and <div> to line breaks, stripping all other tags, and converting escaped characters into their display values.


1 Answers

Django provides an utility function to remove HTML tags:

from django.utils.html import strip_tags  my_string = '<div>Hello, world</div>' my_string = strip_tags(my_string) print(my_string) # Result will be "Hello, world" without the <div> elements 

This function used to be unsafe on older Django version (before 1.7) but nowadays it is completely safe to use it. Here is an article that reviewed this issue when it was relevant.

like image 147
Eldila Avatar answered Oct 01 '22 11:10

Eldila