Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1# Imports
3from django.utils.safestring import mark_safe
4from .base import Element
6# Exports
8__all__ = (
9 "FieldGroup",
10 "Fieldset",
11)
13# Classes
16class FieldGroup(object):
17 """A group of fields."""
19 def __init__(self, *fields, label=None, size=None):
20 """Initialize a field group.
22 :param fields: List of bound or unbound field instances included in the group.
24 :param label: A label for the group.
25 :type label: str
27 :param size: The size of the group's columns.
28 :type size: int
30 """
31 self.fields = list(fields)
32 self.label = label
33 self.size = size
35 def __iter__(self):
36 return iter(self.fields)
38 def __len__(self):
39 return len(self.fields)
42class Fieldset(Element):
43 """A fieldset within a form."""
45 def __init__(self, legend, fields=None, **kwargs):
46 super().__init__("fieldset", **kwargs)
47 self.fields = fields or list()
48 self.legend = legend
50 def __iter__(self):
51 return iter(self.fields)
53 def __len__(self):
54 return len(self.fields)
56 def add(self, field):
57 """Add a field to the fieldset.
59 :param field: The field instance from the form.
60 :type field: BaseType[Field]
62 """
63 self.fields.append(field)
65 @mark_safe
66 def to_html(self):
67 a = list()
68 a.append(self.get_open_tag())
70 a.append("<legend>%s</legend>" % self.legend)
72 for f in self.fields:
73 a.append(str(f))
75 a.append(self.get_close_tag())
77 return "\n".join(a)