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項目範圍來使用我的類來格式化所有錯誤嗎?