0
我写了一个自定义的ErrorList
类:如何在django中使用modelForm的自定义ErrorList类?
class jQueryUiErrors(ErrorList):
def __unicode__(self):
return self.as_divs()
def as_divs(self):
if not self:
return ''
return '<div class="ui-state-error ui-corner-all">%s</div>' % ''.join(['<div class="error"><span style="float: left; margin-right: .3em;" class="ui-icon ui-icon-alert"></span>%s</div>' % e for e in self])
但不能让它开始工作。我想:
class ClientForm(ModelForm):
def __init__(self, *args, **kwargs):
kwargs_new = {'error_class': jQueryUiErrors}
kwargs_new.update(kwargs)
super(ClientForm, self).__init__(*args, **kwargs_new)
class Meta:
model = Client
fields = ['first_name', 'last_name', 'owner', 'source', 'contact_status', 'next_contact']
和:
class ClientForm(ModelForm):
def __init__(self, *args, **kwargs):
self.error_class = jQueryUiErrors
super(ClientForm, self).__init__(*args, **kwargs)
,但没有任何效果 - 错误停留,因为在那里,用它
林在CreateWithInlinesView
呈现为列表,它是基于CreateView
如果与任何事情有关。
PS第二个问题:我可以更改默认的error_class项目范围来使用我的类来格式化所有错误吗?