Malinaizer
Ваши условия тоже не подходят! Задача вроде из простых, может это баг?
>>> # Упростим
>>> def clean_comment(comment):
... num_words = len(comment.split())
... if comment != 'Text' and num_words > 2:
... pass
... else:
... raise Exception('Введите Ваш Текст')
... return comment
>>> clean_comment('Text')
Traceback (most recent call last):
File "<pyshell#10>", line 1, in <module>
clean_comment('Text')
File "<pyshell#9>", line 6, in clean_comment
raise Exception('Введите Ваш Текст')
Exception: Введите Ваш Текст
>>> # OK
>>> clean_comment('Text text text')
2: 'Text text text'
>>> # OK
>>> clean_comment('buzz')
Traceback (most recent call last):
File "<pyshell#14>", line 1, in <module>
clean_comment('buzz')
File "<pyshell#9>", line 6, in clean_comment
raise Exception('Введите Ваш Текст')
Exception: Введите Ваш Текст
>>> # OK
Вроде работает… или я условия неправильно понял?
В чем конкретно неработаемость проявляеться?
PS:
Поигрался с формой уже - работает ведь:
>>> class F(Form):
... name = forms.CharField(initial='Name', label = '', max_length=50, help_text='Today date in text input.')
... comment = forms.CharField(initial='Text',label ='', widget=forms.Textarea)
...
... def clean_comment(self):
... comment = self.cleaned_data['comment']
... num_words = len(comment.split())
... if comment == 'Text' or num_words < 2:
... raise forms.ValidationError(u'Введите Ваш Текст')
... return comment
>>> f = F({'name': 'Booo', 'comment': 'Burururur'})
>>> f.is_valid()
16: False
>>> for k in f.errors:
... print f.errors[k]
<ul class="errorlist"><li>Введите Ваш Текст</li></ul>
>>> f = F({'name': 'Booo', 'comment': 'Text'})
>>> f.is_valid()
17: False
>>> f = F({'name': 'Booo', 'comment': 'Burururur bumbum'})
>>> f.is_valid()
18: True