django-tinymce 小部件未输出正确的错误消息

django-tinymce widget not outputting correct error message

我刚刚设置了 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 小部件?

好吧,多一点解释和几个链接可能有助于阐明这一点。在 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 class 中为模型表单创建自定义 error_messages。由于第一行运行良好,但我认为如果您想以最初发布的方式创建错误消息,您可以尝试按照文档进行操作。在文档中的“覆盖默认字段”部分下,他们展示了一个与您正在做的非常相似的示例,因此应该让您了解可能出了什么问题。我相信这是使用这些 [] 而不是这些的组合() 包装您的字段,然后不覆盖 Meta class 本身中的字段。希望这能让您对 Django 和测试有一些了解!!