我刚刚设置了django-tinymce并对我的表单进行了一些更改。但是,现在我的表单不再输出正确的错误消息。
我的表格:
TITLE_LENGTH_ERROR = "This title is too long, please make it 200 characters or less."
TITLE_EMPTY_ERROR = "You’ll have to add a title."
TEXT_EMPTY_ERROR = "Please enter some text."
NO_CATEGORY_ERROR = "Please select a category."
NO_CITY_ERROR = "Please select a city."
class ArticleForm(ModelForm):
text = forms.CharField(widget=TinyMCE(attrs={'cols': 80, 'rows': 30}))
class Meta:
model = Article
fields = ['title', 'text', 'categories', 'city']
widgets = {'title': forms.TextInput(attrs={
'placeholder': 'Enter a descriptive title'}),
'categories': forms.CheckboxSelectMultiple(choices=Category.CATEGORY_CHOICES),
'city': forms.RadioSelect(choices=City.CITY_CHOICES),
}
error_messages = {
'title': {
'max_length': TITLE_LENGTH_ERROR,
'required': TITLE_EMPTY_ERROR,
},
'text': {
'required': TEXT_EMPTY_ERROR,
},
'categories': {
'required': NO_CATEGORY_ERROR,
},
'city': {
'required': NO_CITY_ERROR,
}
}
测试:
from articles.models import Article, Category, City
from articles.forms import (
ArticleForm,
TITLE_LENGTH_ERROR,
TITLE_EMPTY_ERROR,
TEXT_EMPTY_ERROR,
NO_CATEGORY_ERROR,
NO_CITY_ERROR,
)
class ArticleFormTest(TestCase):
def setUp(self):
self.user = User.objects.create(username='testuser')
self.user.set_password('12345')
self.user.save()
self.client.login(username='testuser', password='12345')
def test_form_validation_for_blank_inputs(self):
form = ArticleForm(data={'title': '', 'text': '', 'categories': '', 'city': '', 'author': self.user})
self.assertFalse(form.is_valid())
self.assertEqual(
form.errors['text'],
[TEXT_EMPTY_ERROR]
)
追溯:
(venv) Robins-MacBook-Pro:togethere robin$ python manage.py test articles/
Creating test database for alias 'default'...
.F....................
======================================================================
FAIL: test_form_validation_for_blank_inputs (articles.tests.test_forms.ArticleFormTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/Users/robin/work/2016-06-04_togethere/togethere/articles/tests/test_forms.py", line 36, in test_form_validation_for_blank_inputs
[TEXT_EMPTY_ERROR]
AssertionError: ['This field is required.'] != ['Please enter some text.']
----------------------------------------------------------------------
Ran 22 tests in 4.171s
FAILED (failures=1)
Destroying test database for alias 'default'...
如何让表单输出正确的错误消息?此外,是否可以以与其他小部件相同的方式声明tinymce小部件?
答案 0 :(得分:1)
好了这么多解释和几个链接可能有助于阐明这一点。在官方的Django文档(https://docs.djangoproject.com/en/1.10/ref/forms/fields/)中,有一个使用表单字段验证和自定义错误消息的示例。必须在您希望与之关联的错误消息的字段中定义error_messages
:
text = forms.CharField(widget=TinyMCE(attrs={'cols': 80, 'rows': 30}), error_messages = { 'required': TEXT_EMPTY_ERROR})
话虽这么说,另一个Django文档(https://docs.djangoproject.com/en/1.10/topics/forms/modelforms/)显示在Meta类中为Model表单创建自定义error_messages。由于第一行的工作非常好,但我认为如果您想以最初发布的方式创建错误消息,可以尝试按照文档进行操作。在文档中的" Overriding Default Fields部分下,他们展示了一个与您正在做的非常类似的示例,这样可以让您了解可能出现的问题。我相信它是使用这些[]
代替这些()
来组合您的字段然后不会覆盖Meta类本身中的字段的组合。希望这能让我们对Django有所了解并进行测试!!