Ticket #4752: div-forms.py

File div-forms.py, 1.2 KB (added by michal@…, 17 years ago)

Sample DivForm class implementation with as_div() shortcut method.

Line 
1##
2## DivForm class
3##
4## Author: Michal Ludvig <michal@logix.cz>
5## http://www.logix.cz/michal
6##
7
8from django import newforms as forms
9
10class DivErrorList(forms.util.ErrorList):
11 """
12 Helper class that wraps error lists into <div> tags for DivForm
13 """
14 def as_div(self):
15 if not self: return u''
16 return u'<div class="errorlist">%s</div>' % ''.join([u'<div class="error">%s</div>' % e for e in self])
17
18 def __str__(self):
19 return self.as_div()
20
21 def __unicode__(self):
22 return self.as_div()
23
24
25class DivForm(forms.Form):
26 """
27 DivForm class that can be used instead of forms.Form
28 to get output fields wrapped in <div> tags instead of
29 those ugly table or <p> tags.
30 """
31 def as_div(self):
32 return self._html_output(u'<div class="form_row"><div class="label">%(label)s</div><div class="field">%(field)s</div><div class="errors">%(errors)s</div><div class="help">%(help_text)s</div></div>',
33 u'<div class="error">%s</div>', '</div>', u'%s', False,
34 DivErrorList)
35
36 def __str__(self):
37 return self.as_div()
38
39 def __unicode__(self):
40 return self.as_div()
Back to Top