Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Django 以与cripsy错误消息相同的样式显示验证错误_Django_Django Crispy Forms - Fatal编程技术网

Django 以与cripsy错误消息相同的样式显示验证错误

Django 以与cripsy错误消息相同的样式显示验证错误,django,django-crispy-forms,Django,Django Crispy Forms,我正在使用crispy表单使我的表单看起来漂亮,并添加了以下验证: forms.py class Meta: widgets = { 'dob': DatePickerInput( options={ "format": "MM/DD/YYYY", "showClose": False, "showClear":

我正在使用crispy表单使我的表单看起来漂亮,并添加了以下验证:

forms.py

class Meta:
    widgets = {
            'dob': DatePickerInput(
                options={
                    "format": "MM/DD/YYYY",
                    "showClose": False,
                    "showClear": False,
                    "showTodayButton": False,
                }
            ),
    }

def clean(self):
        data = self.cleaned_data
        born = data.get('dob')
        today = date.today()
        age = (today.year - born.year - ((today.month, today.day) < (born.month, born.day)))

        if age < 14:
            msg = ("Sorry, you must be atleast 14 years old")
            self.add_error('dob', msg)

        if age > 110:
            msg = ("You entered a date of birth outside of the accepted range. Please try again.")
            self.add_error('dob', msg)

        return data
我的问题是,此错误消息以flash消息的形式显示在页面顶部,而其他消息(我没有设置,但内置在crispy中,例如当用户将必填字段留空时)则显示为与错误相关的字段下的弹出消息框

我想知道如何使我添加的验证错误与其他内置的crispy错误消息相同,以保持一致性

图-()


谢谢。

如果您要覆盖clean方法,则可以使用:


这样就不需要为该字段手动设置样式

非常感谢:)现在该字段变为红色,但不会显示错误消息:(我编辑了我的帖子以提供更多信息。好的,我想是因为
DatePickerInput
而没有显示错误。它与默认的
DateTimeField
一起工作。我刚刚检查过。你能提供
DatePickerInput
链接吗。当然。另外,有可能让错误与此img pl中“性别”字段中的错误相同吗放松?太好了。再次感谢。我很感激:)
dob = models.DateTimeField('Date of birth (mm/dd/yyyy)', null=True, default=now)
def clean(self):
    data = self.cleaned_data
    age = data.get("age")

    if age < 14:
        msg = "Sorry, you must be atleast 14 years old to study with IPC IELTS."
        self.add_error('age', msg)

    if age > 110:
        msg = "You entered a date of birth outside of the accepted range. Please try again."
        self.add_error('age', msg)

    return data
from django.core.validators import MinValueValidator,MaxValueValidator
from django.utils.translation import ugettext_lazy as _

class YourModel(models.Model):
    age = models.DecimalField(..., validators=[
        MinValueValidator(14, message=_("Sorry, you must be atleast 
            14 years old to study with IPC IELTS.")),
        MaxValueValidator(110, message=_("You entered a date of birth outside of the 
            accepted range. Please try again."))
    ])