django forms error_class

不问归期 提交于 2019-12-05 12:05:48
Clément

If you want this behaviour to be common to all your forms, you could have your own form base class defined like that :

class MyBaseForm(forms.Form):
    def __init__(self, *args, **kwargs):
        kwargs_new = {'error_class': DivErrorList}
        kwargs_new.update(kwargs)
        super(MyBaseForm, self).__init__(self, *args, **kwargs_new)

And then have all your form subclass that one. Then all your form will have DivErrorList as a default error renderer, and you will still be able to change it using the error_class argument.

For ModelForm's:

class MyBaseModelForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        kwargs_new = {'error_class': DivErrorList}
        kwargs_new.update(kwargs)
        super(MyBaseModelForm, self).__init__(*args, **kwargs_new)

Try the following:

class MyForm(forms.Form):
    ...

    def __init__(self, *args, **kwargs):
        super(MyForm, self).__init__(*args, **kwargs)
        self.error_class = DivErrorList

Should work. But I did not test it.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!