一、为什么要自定义错误列表样式
django默认的错误列表是<ul class="errorlist">...</ul>可能不符合ui设计样式,因此有必要自定义错误列表样式
二、简例
1.自定义错误列表代码如下
from django.forms.utils import ErrorList class CustomErrorList(ErrorList): def __str__(self): return self.as_p() def as_p(self): if not self: return '' return '%s' % ''.join(['<p class="help-text">%s</p>' % e for e in self])
2.使用
from your.path import CustomErrorList user_form = UserCreationForm(request.POST, error_class=CustomErrorList)
三、参考
https://docs.djangoproject.com/en/2.0/ref/forms/api/#customizing-the-error-list-format