# -*- coding:utf-8 -*-
from django.utils.safestring import mark_safe
from django.utils.translation import ugettext as _
class CustomForm(object):
"""
Inherit with forms.Form or forms.ModelForm
For example:
class AnyForm(forms.Form, CustomForm):
pass
class AnyModelForm(forms.ModelForm, CustomForm):
pass
"""
def render_errors(self):
if not self.errors:
return ""
output = []
output.append(u'
')
output.append(u'
×')
output.append(u'
%s
' % _('You got an error!'))
for field, error in self.errors.items():
output.append(u'- %s %s
' % (field.title(), error[0]))
output.append(u'
')
return mark_safe(u'\n'.join(output))
def as_div(self):
output = []
for boundfield in self: #see original Form class __iter__ method
row_template = u'''
%(label)s
%(field)s
%(help_text)s
'''
row_dict = {
"div_class" : "",
"required_label" : "",
"field" : boundfield.as_widget(),
"label" : boundfield.label_tag(),
"help_text" : boundfield.help_text,
}
if boundfield.errors:
row_dict["div_class"] = "error"
boundfield.field.widget.attrs["class"]="error"
output.append(row_template % row_dict)
return mark_safe(u'\n'.join(output))