Skip to content

Instantly share code, notes, and snippets.

@justinline
Forked from epicserve/html_tags.py
Created November 28, 2017 20:02
Show Gist options
  • Select an option

  • Save justinline/80b2d3d381878db42729da40674c8d8d to your computer and use it in GitHub Desktop.

Select an option

Save justinline/80b2d3d381878db42729da40674c8d8d 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