Django Profanity Filter is a simple Django app that introduces a range of template tags and filters and model validators that remove or censor inappropriate language.
- Install via pip
$ pip install django-profanity-filter
- Add
'profanity',
to yourINSTALLED_APPS
insettings.py
INSTALLED_APPS = (
...
'profanity',
...
)
At the top of every template you wish to use profanity filters and tags on, make sure to load the profanity tags.
...
{% load profanity %}
...
{% with string='You are a bitch!' %}
{{ string|censor }}
{% endwith %}
The output will be You are a *****!
, instead of You are a bitch!
.
{% with string='You are a bitch!' %}
{{ string|is_profane }}
{% endwith %}
The output will be True
, since the string contains profanity.
from profanity.validators import validate_is_profane
class Post(models.Model):
post = models.TextArea(max_length=150, validators=[validate_is_profane])
You can also now validate text within unbroken strings, i.e. strings with no spaces in them (stringswithnospacesinthem). To do so, use the validate_is_profane_nospace validator as shown below:
from profanity.validators import validate_is_profane_nospace
class Post(models.Model):
post = models.TextArea(max_length=150, validators=[validate_is_profane_nospace])
Because there are sometimes profane words nested within safe words (e.g. semen in basement), this validator also uses a "good list" of words that will trigger false positives. If you notice that a word is being blocked from use due to it triggering a false positive, simply add the word to goodlist.txt
- Basic filter
- Advanced filter
- Keyword argument for custom word filter
- Basic Censorship Validator
- Nospace Profanity Validator (for usernames and other unbroken strings)
- Advanced validator - perhaps with the ability to find common morphs of words