28 lines
1.4 KiB
Python
28 lines
1.4 KiB
Python
from django import forms
|
|
from .models import Comment
|
|
from django.core.exceptions import ValidationError
|
|
from django.utils.html import strip_tags
|
|
|
|
|
|
class CommentForm(forms.ModelForm):
|
|
class Meta:
|
|
model = Comment
|
|
fields = ['author_name', 'author_email', 'content']
|
|
widgets = {
|
|
'author_name': forms.TextInput(attrs={'class': 'form-input', 'placeholder': 'Ваше имя'}),
|
|
'author_email': forms.EmailInput(attrs={'class': 'form-input', 'placeholder': 'Ваш email'}),
|
|
'content': forms.Textarea(attrs={'class': 'form-textarea', 'rows': 4, 'placeholder': 'Текст комментария',
|
|
'autocomplete': 'off'}),
|
|
}
|
|
|
|
def clean_content(self):
|
|
content = self.cleaned_data['content']
|
|
# Удаляем HTML-теги и проверяем, остался ли текст
|
|
clean_text = strip_tags(content)
|
|
if not clean_text.strip():
|
|
raise ValidationError("Комментарий не должен состоять только из HTML-тегов")
|
|
# Если хотите запретить любые теги, можно сравнить content и clean_text
|
|
if content != clean_text:
|
|
raise ValidationError("HTML-теги в комментариях запрещены")
|
|
return content
|