如何覆盖默认的Django表单验证错误

时间:2019-04-24 07:41:52

标签: django django-forms django-validation

我尝试了一下,但是没有用。仅显示默认错误消息。请帮助............

from django import forms
from SharedApps_Application.models import certificateDb
from django.contrib.admin.widgets import AdminDateWidget
from django.forms.fields import DateField

my_default_errors = {
    'required': 'Application field is required',
}

class CertificateForm(forms.ModelForm):

    startdate = forms.DateField(widget = forms.SelectDateWidget(years=range(1995, 2100)))
    expiredate = forms.DateField(widget = forms.SelectDateWidget(years=range(1995, 2100)))
    application = forms.CharField(error_messages = my_default_errors)
    class Meta:
        model = certificateDb
        fields = ('application', 'startdate', 'expiredate', 'environment_type','File' )
  

屏幕截图

[1]: https://i.stack.imgur.com/8zhM6.png

  

更新

from django import forms
from SharedApps_Application.models import certificateDb
from django.contrib.admin.widgets import AdminDateWidget
from django.forms.fields import DateField
class CertificateForm(forms.ModelForm):

    startdate = forms.DateField(widget = forms.SelectDateWidget(years=range(1995, 2100)))
    expiredate = forms.DateField(widget = forms.SelectDateWidget(years=range(1995, 2100)))
    application = forms.CharField()
    class Meta:
        model = certificateDb
        fields = ('application', 'startdate', 'expiredate', 'environment_type','File' )

        error_messages = {
            'application': {
                'required': ("Application field is required"),
            },
            }

1 个答案:

答案 0 :(得分:0)

基于刚添加的屏幕截图,您想要更改浏览器呈现给客户端的错误消息。这可以通过向您的字段添加oninvalid属性来完成。

这是在forms.py文件中完成的方式:

from django import forms
from SharedApps_Application.models import certificateDb
from django.contrib.admin.widgets import AdminDateWidget
from django.forms.fields import DateField


class CertificateForm(forms.ModelForm):
    app_attributes = {'oninvalid': 'this.setCustomValidity("Application field is required")', 'oninput': 'this.setCustomValidity("")'}
    startdate = forms.DateField(widget = forms.SelectDateWidget(years=range(1995, 2100)))
    expiredate = forms.DateField(widget = forms.SelectDateWidget(years=range(1995, 2100)))
    application = forms.CharField(widget=forms.TextInput(attrs=app_attributes)
    class Meta:
        model = certificateDb
        fields = ('application', 'startdate', 'expiredate', 'environment_type','File' )

        error_messages = {
            'application': {
                'required': ("Application field is required"),
            },
            }

假设您要覆盖的字段为'application'。像这样在您的类Meta下添加一个error_messages字典:

旧答案-覆盖Django表单验证错误消息

from django import forms
from SharedApps_Application.models import certificateDb
from django.contrib.admin.widgets import AdminDateWidget
from django.forms.fields import DateField


class CertificateForm(forms.ModelForm):

    startdate = forms.DateField(widget = forms.SelectDateWidget(years=range(1995, 2100)))
    expiredate = forms.DateField(widget = forms.SelectDateWidget(years=range(1995, 2100)))
    application = forms.CharField()
    class Meta:
        model = certificateDb
        fields = ('application', 'startdate', 'expiredate', 'environment_type','File' )

        error_messages = {
            'application': {
                'required': ("Application field is required"),
            },
            }