Skip to content

Instantly share code, notes, and snippets.

@epicserve
Created September 17, 2014 19:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save epicserve/f05407fd02889b6f2752 to your computer and use it in GitHub Desktop.
Save epicserve/f05407fd02889b6f2752 to your computer and use it in GitHub Desktop.
Example of using bleach to strip out bad/evil code.
from django import template
from django.utils.safestring import mark_safe
import bleach
register = template.Library()
@register.filter
def strip_tags(text, valid_tags=['p', 'a', 'strong', 'em', 'ol', 'ul', 'li']):
if not isinstance(valid_tags, list):
valid_tags = valid_tags.split(',')
text = bleach.clean(text, valid_tags)
return mark_safe(text)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment