Wagtail领域的自定义验证器

时间:2017-09-05 22:57:31

标签: django django-forms wagtail

我有一个.inner { position: absolute; } span{ visibility:hidden; } .blue{ background:blue; } .green{ background:green; } .yellow{ background:yellow; } .red{ background:red; } 我想添加一个<div class="outer"> <div class="inner blue"><span>inner</span></div> <div class="inner green"><span>inner</span></div> <div class="inner yellow"><span>inner</span></div> <div class="inner red"><span>inner</span></div> </div>

根据Django文档,您可以使用内置的验证器来强制执行此操作(https://docs.djangoproject.com/en/1.11/ref/validators/#minlengthvalidator)。

我尝试使用此代码将其添加到我的模型中:

TextField

但是,当发布此页面的值低于240时,我没有收到预期的验证错误,而是点击min_lengthclass MyPage(Page): ... introduction = models.TextField( null=True, blank=False, validators=[MinLengthValidator(240, message="Must be at least 240 chars.")]

有没有办法TypeError上有unorderable types: str() < int()

1 个答案:

答案 0 :(得分:0)

如果默认MinLengthValidator不适合您,我建议您创建一个自定义验证器,如下所示:

from django.core.validators import ValidationError

def max_length_validator(value):
    if len(value) < 240:
        raise ValidationError("Must be at least 240 chars.")


class MyPage(Page):
    introduction = models.TextField(
        blank=False, validators=[max_length_validator])