Z
Z
Zuzupoj2020-04-26 09:14:15
Django
Zuzupoj, 2020-04-26 09:14:15

How to make a word filter in django?

There is a form for entering mail, name, subject and text. How to make a filter so that the user cannot send text, for example, with a swear word?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir, 2020-04-26
@Zuzupoj

Add a clean-method to check the desired field to the form class :

STOP_LIST = [
    'мат',
    'мат',
    'мат',
]

class ContactForm(forms.Form):
    ...

    def clean_text(self):
        text = self.cleaned_data['text']
        for word in STOP_LIST:
            if word in text:
                raise forms.ValidationError("Вы позволили себе немного лишнего! Одумайтесь и исправьте текст!")
        return text

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question