Changing the look of newforms as_table with a custom BaseForm

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
from django import newforms as forms


class PrettyBaseForm (forms.BaseForm):

    class NormalRowFormatter(object):

        def _error_class(self, errors):
            if errors:
                return ' class="error"'
            else:
                return ''

        def __mod__(self, d):
            return u'<tr%s>' % self._error_class(d['errors']) \
                 + u'<th>%(label)s</th><td>%(field)s%(errors)s%(help_text)s</td></tr>' % d

    def as_table(self):
        return self._html_output(
            PrettyBaseForm.NormalRowFormatter(),
            u'<tr><td colspan="2">%s</td></tr>', # unused
            u'</td></tr>',
            u'<br />%s',
            False
        )

More like this

  1. custom css classes for newforms by robharvey 6 years, 11 months ago
  2. FieldsetForm by Ciantic 7 years ago
  3. load m2m fields objects by dirol 3 years, 10 months ago
  4. Form rendering using a template instead of builtin HTML by leoh 6 years, 10 months ago
  5. Form row filter by kylefox 6 years, 2 months ago

Comments

(Forgotten your password?)