=== modified file 'django/newforms/extras/widgets.py'
--- django/newforms/extras/widgets.py	2008-01-28 15:22:01 +0000
+++ django/newforms/extras/widgets.py	2008-01-28 17:45:19 +0000
@@ -21,9 +21,9 @@
     day_field = '%s_day'
     year_field = '%s_year'
 
-    def __init__(self, attrs=None, years=None):
+    def __init__(self, attrs=None, row_attrs=None, years=None):
         # years is an optional list/tuple of years to use in the "year" select box.
-        self.attrs = attrs or {}
+        super(SelectDateWidget, self).__init__(attrs, row_attrs)
         if years:
             self.years = years
         else:

=== modified file 'django/newforms/fields.py'
--- django/newforms/fields.py	2008-01-28 15:22:01 +0000
+++ django/newforms/fields.py	2008-01-28 17:45:19 +0000
@@ -513,7 +513,6 @@
             return value
         if self.verify_exists:
             import urllib2
-            from django.conf import settings
             headers = {
                 "Accept": "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5",
                 "Accept-Language": "en-us,en;q=0.5",

=== modified file 'django/newforms/forms.py'
--- django/newforms/forms.py	2008-02-16 07:59:58 +0000
+++ django/newforms/forms.py	2008-02-18 14:55:31 +0000
@@ -4,8 +4,8 @@
 
 from copy import deepcopy
 
-from django.utils.datastructures import SortedDict
-from django.utils.html import escape
+from django.utils.datastructures import SortedDict, InheritableOptions
+from django.utils.html import conditional_escape
 from django.utils.encoding import StrAndUnicode, smart_unicode, force_unicode
 from django.utils.safestring import mark_safe
 
@@ -22,41 +22,67 @@
     name = name[0].upper() + name[1:]
     return name.replace('_', ' ')
 
-def get_declared_fields(bases, attrs, with_base_fields=True):
-    """
-    Create a list of form field instances from the passed in 'attrs', plus any
-    similar fields on the base classes (in 'bases'). This is used by both the
-    Form and ModelForm metclasses.
-
-    If 'with_base_fields' is True, all fields from the bases are used.
-    Otherwise, only fields in the 'declared_fields' attribute on the bases are
-    used. The distinction is useful in ModelForm subclassing.
-    """
-    fields = [(field_name, attrs.pop(field_name)) for field_name, obj in attrs.items() if isinstance(obj, Field)]
-    fields.sort(lambda x, y: cmp(x[1].creation_counter, y[1].creation_counter))
-
-    # If this class is subclassing another Form, add that Form's fields.
-    # Note that we loop over the bases in *reverse*. This is necessary in
-    # order to preserve the correct order of fields.
-    if with_base_fields:
-        for base in bases[::-1]:
-            if hasattr(base, 'base_fields'):
-                fields = base.base_fields.items() + fields
-    else:
-        for base in bases[::-1]:
-            if hasattr(base, 'declared_fields'):
-                fields = base.declared_fields.items() + fields
-
-    return SortedDict(fields)
-
-class DeclarativeFieldsMetaclass(type):
-    """
-    Metaclass that converts Field attributes to a dictionary called
-    'base_fields', taking into account parent class 'base_fields' as well.
-    """
+class FormOptions(InheritableOptions):
+    _default_options = {
+        'fieldsets': None,
+        'fields': None,
+        'exclude': None,
+    }
+
+class FormMetaclass(type):
+
+    def create_options(cls, new_cls):
+        new_cls._meta = new_cls.options(new_cls)
+        try:
+            delattr(new_cls, 'Meta')
+        except AttributeError:
+            pass
+    create_options = classmethod(create_options)
+
+    def create_declared_fields(cls, new_cls):
+        fields = []
+        for name, attr in new_cls.__dict__.items():
+            if isinstance(attr, Field):
+                fields.append((name, attr))
+                delattr(new_cls, name)
+        fields.sort(lambda x, y: cmp(x[1].creation_counter, y[1].creation_counter))
+        new_cls._declared_fields = SortedDict(fields)
+    create_declared_fields = classmethod(create_declared_fields)
+
+    def create_base_fields_pool_from_declared_fields(cls, new_cls):
+        fields = []
+        # Add all declared fields from this class and from superclasses. Note
+        # that we loop over the bases in *reverse*. This is necessary
+        # in order to preserve the correct order of fields.
+        for base in new_cls.__mro__[::-1]:
+            try:
+                fields += base._declared_fields.items()
+            except AttributeError:
+                pass
+        new_cls._base_fields_pool = SortedDict(fields)
+    create_base_fields_pool_from_declared_fields = classmethod(create_base_fields_pool_from_declared_fields)
+
+    def create_base_fields_from_base_fields_pool(cls, new_cls):
+        if new_cls._meta.fieldsets:
+            names = []
+            for fieldset in new_cls._meta.fieldsets:
+                names.extend(fieldset['fields'])
+        elif new_cls._meta.fields:
+            names = new_cls._meta.fields
+        elif new_cls._meta.exclude:
+            names = [name for name in new_cls._base_fields_pool if name not in new_cls._meta.exclude]
+        else:
+            names = new_cls._base_fields_pool.keys()
+        new_cls.base_fields = SortedDict([(name, new_cls._base_fields_pool[name]) for name in names])
+    create_base_fields_from_base_fields_pool = classmethod(create_base_fields_from_base_fields_pool)
+
     def __new__(cls, name, bases, attrs):
-        attrs['base_fields'] = get_declared_fields(bases, attrs)
-        return type.__new__(cls, name, bases, attrs)
+        new_cls = type.__new__(cls, name, bases, attrs)
+        cls.create_options(new_cls)
+        cls.create_declared_fields(new_cls)
+        cls.create_base_fields_pool_from_declared_fields(new_cls)
+        cls.create_base_fields_from_base_fields_pool(new_cls)
+        return new_cls
 
 class BaseForm(StrAndUnicode):
     # This is the main implementation of all the Form logic. Note that this
@@ -98,7 +124,7 @@
         return BoundField(self, field, name)
 
     def _get_errors(self):
-        "Returns an ErrorDict for the data provided for the form"
+        "Returns an ErrorDict for the data provided for the form."
         if self._errors is None:
             self.full_clean()
         return self._errors
@@ -111,70 +137,99 @@
         """
         return self.is_bound and not bool(self.errors)
 
-    def add_prefix(self, field_name):
+    def has_fieldsets(self):
+        "Returns True if this form has fieldsets."
+        return bool(self._meta.fieldsets)
+
+    def add_prefix(self, name):
         """
         Returns the field name with a prefix appended, if this Form has a
         prefix set.
 
         Subclasses may wish to override.
         """
-        return self.prefix and ('%s-%s' % (self.prefix, field_name)) or field_name
-
-    def _html_output(self, normal_row, error_row, row_ender, help_text_html, errors_on_separate_row):
+        return self.prefix and ('%s-%s' % (self.prefix, name)) or name
+
+    def first_fieldset_attributes(self):
+        "Returns attributes for first fieldset as HTML code."
+        if self.has_fieldsets() and 'attrs' in self._meta.fieldsets[0]:
+            return flatatt(self._meta.fieldsets[0]['attrs'])
+        else:
+            return u''
+
+    def first_fieldset_legend_tag(self):
+        "Returns legend tag for first fieldset as HTML code."
+        if self.has_fieldsets() and 'legend' in self._meta.fieldsets[0]:
+            return mark_safe(u'<legend>%s</legend>' % conditional_escape(force_unicode(self._meta.fieldsets[0]['legend'])))
+        else:
+            return u''
+
+    def top_errors_html_output(self, top_errors, top_errors_row):
+        "Helper function for outputting HTML from a top errors row. Used by _html_output."
+        return top_errors_row % top_errors
+
+    def fieldset_html_output(self, fieldset, fields, fieldset_start, fieldset_end, is_first, is_last):
+        "Helper function for outputting HTML from a fieldset. Used by _html_output."
+        output = []
+        if fieldset_start and not is_first:
+            fieldset_attrs = flatatt(fieldset.get('attrs', {}))
+            if 'legend' in fieldset:
+                legend_tag = u'\n<legend>%s</legend>' % conditional_escape(force_unicode(fieldset['legend']))
+            else:
+                legend_tag = u''
+            output.append(fieldset_start % (u'<fieldset%s>%s' % (fieldset_attrs, legend_tag)))
+        for name in fieldset['fields']:
+            output.append(fields[name])
+        if fieldset_end and not is_last:
+            output.append(fieldset_end % u'</fieldset>')
+        return u'\n'.join(output)
+
+    def hidden_fields_html_output(self, hidden_fields, hidden_fields_row):
+        "Helper function for outputting HTML from a hidden fields row. Used by _html_output."
+        return hidden_fields_row % u''.join(hidden_fields)
+
+    def _html_output(self, output_type, top_errors_row, fieldset_start, fieldset_end, hidden_fields_row):
         "Helper function for outputting HTML. Used by as_table(), as_ul(), as_p()."
+        output = []
         top_errors = self.non_field_errors() # Errors that should be displayed above all fields.
-        output, hidden_fields = [], []
+        hidden_fields, visible_fields = [], {}
         for name, field in self.fields.items():
             bf = BoundField(self, field, name)
-            bf_errors = self.error_class([escape(error) for error in bf.errors]) # Escape and cache in local variable.
             if bf.is_hidden:
-                if bf_errors:
-                    top_errors.extend(['(Hidden field %s) %s' % (name, force_unicode(e)) for e in bf_errors])
+                if bf.errors:
+                    top_errors.extend(['(Hidden field %s) %s' % (name, conditional_escape(force_unicode(e))) for e in bf.errors])
                 hidden_fields.append(unicode(bf))
             else:
-                if errors_on_separate_row and bf_errors:
-                    output.append(error_row % force_unicode(bf_errors))
-                if bf.label:
-                    label = escape(force_unicode(bf.label))
-                    # Only add the suffix if the label does not end in
-                    # punctuation.
-                    if self.label_suffix:
-                        if label[-1] not in ':?.!':
-                            label += self.label_suffix
-                    label = bf.label_tag(label) or ''
-                else:
-                    label = ''
-                if field.help_text:
-                    help_text = help_text_html % force_unicode(field.help_text)
-                else:
-                    help_text = u''
-                output.append(normal_row % {'errors': force_unicode(bf_errors), 'label': force_unicode(label), 'field': unicode(bf), 'help_text': help_text})
+                visible_fields[name] = getattr(bf.widget, 'for_%s' % output_type)(rendered_widget=unicode(bf),
+                    rendered_errors=unicode(bf.errors), label_tag=bf.label_tag, help_text=bf.help_text, row_attrs=bf.row_attrs)
         if top_errors:
-            output.insert(0, error_row % top_errors)
-        if hidden_fields: # Insert any hidden fields in the last row.
-            str_hidden = u''.join(hidden_fields)
-            if output:
-                last_row = output[-1]
-                # Chop off the trailing row_ender (e.g. '</td></tr>') and
-                # insert the hidden fields.
-                output[-1] = last_row[:-len(row_ender)] + str_hidden + row_ender
-            else:
-                # If there aren't any rows in the output, just append the
-                # hidden fields.
-                output.append(str_hidden)
+            output.append(self.top_errors_html_output(top_errors, top_errors_row))
+        if self.has_fieldsets():
+            for i, fieldset in enumerate(self._meta.fieldsets):
+                output_method = fieldset.get('html_output_method', self.__class__.fieldset_html_output)
+                fields = dict((name, visible_fields[name]) for name in fieldset['fields'] if name in visible_fields)
+                is_first = (i == 0)
+                is_last = (i + 1 == len(self._meta.fieldsets))
+                output.append(output_method(self, fieldset, fields, fieldset_start, fieldset_end, is_first, is_last))
+        else:
+            for name in self.fields:
+                if name in visible_fields:
+                    output.append(visible_fields[name])
+        if hidden_fields:
+            output.append(self.hidden_fields_html_output(hidden_fields, hidden_fields_row))
         return mark_safe(u'\n'.join(output))
 
     def as_table(self):
         "Returns this form rendered as HTML <tr>s -- excluding the <table></table>."
-        return self._html_output(u'<tr><th>%(label)s</th><td>%(errors)s%(field)s%(help_text)s</td></tr>', u'<tr><td colspan="2">%s</td></tr>', '</td></tr>', u'<br />%s', False)
+        return self._html_output('table', u'<tr><td colspan="2">%s</td></tr>', u'%s\n<table>', u'</table>\n%s', u'<tr><td colspan="2">%s</td></tr>')
 
     def as_ul(self):
         "Returns this form rendered as HTML <li>s -- excluding the <ul></ul>."
-        return self._html_output(u'<li>%(errors)s%(label)s %(field)s%(help_text)s</li>', u'<li>%s</li>', '</li>', u' %s', False)
+        return self._html_output('ul', u'<li>%s</li>', u'%s\n<ul>', u'</ul>\n%s', u'<li>%s</li>')
 
     def as_p(self):
         "Returns this form rendered as HTML <p>s."
-        return self._html_output(u'<p>%(label)s %(field)s%(help_text)s</p>', u'%s', '</p>', u' %s', True)
+        return self._html_output('p', u'%s', u'%s', u'%s', u'<p>%s</p>')
 
     def non_field_errors(self):
         """
@@ -209,13 +264,13 @@
                     value = getattr(self, 'clean_%s' % name)()
                     self.cleaned_data[name] = value
             except ValidationError, e:
-                self._errors[name] = e.messages
+                self._errors[name] = self.error_class(e.messages)
                 if name in self.cleaned_data:
                     del self.cleaned_data[name]
         try:
             self.cleaned_data = self.clean()
         except ValidationError, e:
-            self._errors[NON_FIELD_ERRORS] = e.messages
+            self._errors[NON_FIELD_ERRORS] = self.error_class(e.messages)
         if self._errors:
             delattr(self, 'cleaned_data')
 
@@ -245,20 +300,17 @@
     # fancy metaclass stuff purely for the semantic sugar -- it allows one
     # to define a form using declarative syntax.
     # BaseForm itself has no way of designating self.fields.
-    __metaclass__ = DeclarativeFieldsMetaclass
+    __metaclass__ = FormMetaclass
+    options = FormOptions
 
 class BoundField(StrAndUnicode):
     "A Field plus data"
     def __init__(self, form, field, name):
         self.form = form
         self.field = field
+        self.widget = field.widget
         self.name = name
         self.html_name = form.add_prefix(name)
-        if self.field.label is None:
-            self.label = pretty_name(name)
-        else:
-            self.label = self.field.label
-        self.help_text = field.help_text or ''
 
     def __unicode__(self):
         """Renders this field as an HTML widget."""
@@ -279,7 +331,7 @@
         field's default widget will be used.
         """
         if not widget:
-            widget = self.field.widget
+            widget = self.widget
         attrs = attrs or {}
         auto_id = self.auto_id
         if auto_id and 'id' not in attrs and 'id' not in widget.attrs:
@@ -312,28 +364,48 @@
         """
         Returns the data for this BoundField, or None if it wasn't given.
         """
-        return self.field.widget.value_from_datadict(self.form.data, self.form.files, self.html_name)
+        return self.widget.value_from_datadict(self.form.data, self.form.files, self.html_name)
     data = property(_data)
 
-    def label_tag(self, contents=None, attrs=None):
-        """
-        Wraps the given contents in a <label>, if the field has an ID attribute.
-        Does not HTML-escape the contents. If contents aren't given, uses the
-        field's HTML-escaped label.
-
-        If attrs are given, they're used as HTML attributes on the <label> tag.
-        """
-        contents = contents or escape(self.label)
-        widget = self.field.widget
-        id_ = widget.attrs.get('id') or self.auto_id
-        if id_:
-            attrs = attrs and flatatt(attrs) or ''
-            contents = '<label for="%s"%s>%s</label>' % (widget.id_for_label(id_), attrs, contents)
-        return mark_safe(contents)
+    def _label(self):
+        "Returns label for this field as safe HTML code."
+        if self.field.label is None:
+            return pretty_name(self.name)
+        else:
+            return conditional_escape(force_unicode(self.field.label))
+    label = property(_label)
+
+    def _label_tag(self):
+        "Returns label tag for this field as safe HTML code."
+        label = self.label
+        if self.form.label_suffix:
+            # Only add the suffix if the label does not end in punctuation.
+            if label and label[-1] not in ':?.!':
+                label += self.form.label_suffix
+        id_ = self.widget.attrs.get('id') or self.auto_id
+        if label and id_:
+            id_ = self.widget.id_for_label(id_)
+            return mark_safe(self.widget.label_tag(id_, label))
+        else:
+            return label
+    label_tag = property(_label_tag)
+
+    def _help_text(self):
+        "Returns help text for this field as safe HTML code."
+        if self.field.help_text:
+            return force_unicode(self.field.help_text)
+        else:
+            return u''
+    help_text = property(_help_text)
+
+    def _row_attrs(self):
+        "Returns row attributes for this field as safe HTML code."
+        return flatatt(self.widget.row_attrs)
+    row_attrs = property(_row_attrs)
 
     def _is_hidden(self):
         "Returns True if this BoundField's widget is hidden."
-        return self.field.widget.is_hidden
+        return self.widget.is_hidden
     is_hidden = property(_is_hidden)
 
     def _auto_id(self):

=== modified file 'django/newforms/models.py'
--- django/newforms/models.py	2008-02-16 07:59:58 +0000
+++ django/newforms/models.py	2008-02-18 14:53:53 +0000
@@ -7,11 +7,10 @@
 
 from django.utils.translation import ugettext_lazy as _
 from django.utils.encoding import smart_unicode
-from django.utils.datastructures import SortedDict
-from django.core.exceptions import ImproperlyConfigured
+from django.utils.datastructures import SortedDict, InheritableOptions
 
 from util import ValidationError, ErrorList
-from forms import BaseForm, get_declared_fields
+from forms import FormOptions, FormMetaclass, BaseForm
 from fields import Field, ChoiceField, EMPTY_VALUES
 from widgets import Select, SelectMultiple, MultipleHiddenInput
 
@@ -205,40 +204,52 @@
             field_list.append((f.name, formfield))
     return SortedDict(field_list)
 
-class ModelFormOptions(object):
-    def __init__(self, options=None):
-        self.model = getattr(options, 'model', None)
-        self.fields = getattr(options, 'fields', None)
-        self.exclude = getattr(options, 'exclude', None)
-
-
-class ModelFormMetaclass(type):
-    def __new__(cls, name, bases, attrs,
-                formfield_callback=lambda f: f.formfield()):
-        try:
-            parents = [b for b in bases if issubclass(b, ModelForm)]
-        except NameError:
-            # We are defining ModelForm itself.
-            parents = None
-        if not parents:
-            return super(ModelFormMetaclass, cls).__new__(cls, name, bases,
-                    attrs)
-
-        new_class = type.__new__(cls, name, bases, attrs)
-        declared_fields = get_declared_fields(bases, attrs, False)
-        opts = new_class._meta = ModelFormOptions(getattr(new_class, 'Meta', None))
-        if opts.model:
-            # If a model is defined, extract form fields from it.
-            fields = fields_for_model(opts.model, opts.fields,
-                                      opts.exclude, formfield_callback)
-            # Override default model fields with any custom declared ones
-            # (plus, include all the other declared fields).
-            fields.update(declared_fields)
-        else:
-            fields = declared_fields
-        new_class.declared_fields = declared_fields
-        new_class.base_fields = fields
-        return new_class
+class ModelFormOptions(InheritableOptions):
+    _model_options = {
+        'model': None,
+        'formfield_for_dbfield': lambda self, dbfield: dbfield.formfield(),
+    }
+    _default_options = FormOptions._default_options.copy()
+    _default_options.update(_model_options)
+
+class ModelFormMetaclass(FormMetaclass):
+
+    def create_model_fields(cls, new_cls):
+        fields = []
+        if new_cls._meta.model:
+            for dbfield in new_cls._meta.model._meta.fields + new_cls._meta.model._meta.many_to_many:
+                if dbfield.editable:
+                    formfield = new_cls._meta.formfield_for_dbfield(new_cls._meta, dbfield)
+                    if formfield:
+                        fields.append((dbfield.name, formfield))
+        new_cls._model_fields = SortedDict(fields)
+    create_model_fields = classmethod(create_model_fields)
+
+    def create_base_fields_pool_from_model_fields_and_declared_fields(cls, new_cls):
+        model_fields = []
+        declared_fields = []
+        # Add all declared fields from this class and from superclasses. Note
+        # that we loop over the bases in *reverse*. This is necessary
+        # in order to preserve the correct order of fields. Also add first
+        # model fields.
+        for base in new_cls.__mro__[::-1]:
+            try:
+                declared_fields += base._declared_fields.items()
+                if base._meta.model:
+                    model_fields = base._model_fields.items()
+            except AttributeError:
+                pass
+        new_cls._base_fields_pool = SortedDict(model_fields + declared_fields)
+    create_base_fields_pool_from_model_fields_and_declared_fields = classmethod(create_base_fields_pool_from_model_fields_and_declared_fields)
+
+    def __new__(cls, name, bases, attrs):
+        new_cls = type.__new__(cls, name, bases, attrs)
+        cls.create_options(new_cls)
+        cls.create_model_fields(new_cls)
+        cls.create_declared_fields(new_cls)
+        cls.create_base_fields_pool_from_model_fields_and_declared_fields(new_cls)
+        cls.create_base_fields_from_base_fields_pool(new_cls)
+        return new_cls
 
 class BaseModelForm(BaseForm):
     def __init__(self, data=None, files=None, auto_id='id_%s', prefix=None,
@@ -251,7 +262,7 @@
             object_data = {}
         else:
             self.instance = instance
-            object_data = model_to_dict(instance, opts.fields, opts.exclude)
+            object_data = model_to_dict(instance, self.base_fields.keys())
         # if initial was provided, it should override the values from instance
         if initial is not None:
             object_data.update(initial)
@@ -269,10 +280,11 @@
             fail_message = 'created'
         else:
             fail_message = 'changed'
-        return save_instance(self, self.instance, self._meta.fields, fail_message, commit)
+        return save_instance(self, self.instance, self.base_fields.keys(), fail_message, commit)
 
 class ModelForm(BaseModelForm):
     __metaclass__ = ModelFormMetaclass
+    options = ModelFormOptions
 
 
 # Fields #####################################################################

=== modified file 'django/newforms/util.py'
--- django/newforms/util.py	2008-01-28 15:22:01 +0000
+++ django/newforms/util.py	2008-01-30 13:34:53 +0000
@@ -1,6 +1,5 @@
-from django.utils.html import escape
+from django.utils.html import conditional_escape
 from django.utils.encoding import smart_unicode, StrAndUnicode, force_unicode
-from django.utils.functional import Promise
 from django.utils.safestring import mark_safe
 
 def flatatt(attrs):
@@ -10,7 +9,7 @@
     XML-style pairs.  It is assumed that the keys do not need to be XML-escaped.
     If the passed dictionary is empty, then return an empty string.
     """
-    return u''.join([u' %s="%s"' % (k, escape(v)) for k, v in attrs.items()])
+    return mark_safe(u''.join([u' %s="%s"' % (k, conditional_escape(v)) for k, v in attrs.items()]))
 
 class ErrorDict(dict, StrAndUnicode):
     """
@@ -24,7 +23,7 @@
     def as_ul(self):
         if not self: return u''
         return mark_safe(u'<ul class="errorlist">%s</ul>'
-                % ''.join([u'<li>%s%s</li>' % (k, force_unicode(v))
+                % ''.join([u'<li>%s%s</li>' % (k, conditional_escape(force_unicode(v)))
                     for k, v in self.items()]))
 
     def as_text(self):
@@ -40,7 +39,7 @@
     def as_ul(self):
         if not self: return u''
         return mark_safe(u'<ul class="errorlist">%s</ul>'
-                % ''.join([u'<li>%s</li>' % force_unicode(e) for e in self]))
+                % ''.join([u'<li>%s</li>' % conditional_escape(force_unicode(e)) for e in self]))
 
     def as_text(self):
         if not self: return u''

=== modified file 'django/newforms/widgets.py'
--- django/newforms/widgets.py	2008-02-05 21:49:42 +0000
+++ django/newforms/widgets.py	2008-02-07 20:54:54 +0000
@@ -29,15 +29,20 @@
     is_hidden = False          # Determines whether this corresponds to an <input type="hidden">.
     needs_multipart_form = False # Determines does this widget need multipart-encrypted form
 
-    def __init__(self, attrs=None):
+    def __init__(self, attrs=None, row_attrs=None):
         if attrs is not None:
             self.attrs = attrs.copy()
         else:
             self.attrs = {}
+        if row_attrs is not None:
+            self.row_attrs = row_attrs.copy()
+        else:
+            self.row_attrs = {}
 
     def __deepcopy__(self, memo):
         obj = copy.copy(self)
         obj.attrs = self.attrs.copy()
+        obj.row_attrs = self.row_attrs.copy()
         memo[id(self)] = obj
         return obj
 
@@ -77,6 +82,28 @@
         return id_
     id_for_label = classmethod(id_for_label)
 
+    def label_tag(self, id_, label):
+        "Returns label rendered as HTML <label>."
+        return u'<label for="%s">%s</label>' % (id_, label)
+
+    def for_table(self, rendered_widget, rendered_errors, label_tag, help_text, row_attrs):
+        "Returns this widget rendered as HTML <tr>."
+        if help_text:
+            help_text = u'<br />%s' % help_text
+        return u'<tr%(row_attrs)s><th>%(label_tag)s</th><td>%(rendered_errors)s%(rendered_widget)s%(help_text)s</td></tr>' % locals()
+
+    def for_ul(self, rendered_widget, rendered_errors, label_tag, help_text, row_attrs):
+        "Returns this widget rendered as HTML <li>."
+        if help_text:
+            help_text = u' %s' % help_text
+        return u'<li%(row_attrs)s>%(rendered_errors)s%(label_tag)s %(rendered_widget)s%(help_text)s</li>' % locals()
+
+    def for_p(self, rendered_widget, rendered_errors, label_tag, help_text, row_attrs):
+        "Returns this widget rendered as HTML <p>."
+        if help_text:
+            help_text = u' %s' % help_text
+        return u'%(rendered_errors)s<p%(row_attrs)s>%(label_tag)s %(rendered_widget)s%(help_text)s</p>' % locals()
+
 class Input(Widget):
     """
     Base class for all <input> widgets (except type='checkbox' and
@@ -98,8 +125,8 @@
 class PasswordInput(Input):
     input_type = 'password'
 
-    def __init__(self, attrs=None, render_value=True):
-        super(PasswordInput, self).__init__(attrs)
+    def __init__(self, attrs=None, row_attrs=None, render_value=True):
+        super(PasswordInput, self).__init__(attrs, row_attrs)
         self.render_value = render_value
 
     def render(self, name, value, attrs=None):
@@ -115,8 +142,8 @@
     A widget that handles <input type="hidden"> for fields that have a list
     of values.
     """
-    def __init__(self, attrs=None, choices=()):
-        super(MultipleHiddenInput, self).__init__(attrs)
+    def __init__(self, attrs=None, row_attrs=None, choices=()):
+        super(MultipleHiddenInput, self).__init__(attrs, row_attrs)
         # choices can be any iterable
         self.choices = choices
 
@@ -144,11 +171,12 @@
         return files.get(name, None)
 
 class Textarea(Widget):
-    def __init__(self, attrs=None):
+    def __init__(self, attrs=None, row_attrs=None):
         # The 'rows' and 'cols' attributes are required for HTML correctness.
-        self.attrs = {'cols': '40', 'rows': '10'}
+        default_attrs = {'cols': '40', 'rows': '10'}
         if attrs:
-            self.attrs.update(attrs)
+            default_attrs.update(attrs)
+        super(Textarea, self).__init__(default_attrs, row_attrs)
 
     def render(self, name, value, attrs=None):
         if value is None: value = ''
@@ -161,8 +189,8 @@
     input_type = 'text'
     format = '%Y-%m-%d %H:%M:%S'     # '2006-10-25 14:30:59'
 
-    def __init__(self, attrs=None, format=None):
-        super(DateTimeInput, self).__init__(attrs)
+    def __init__(self, attrs=None, row_attrs=None, format=None):
+        super(DateTimeInput, self).__init__(attrs, row_attrs)
         if format:
             self.format = format
 
@@ -174,8 +202,8 @@
         return super(DateTimeInput, self).render(name, value, attrs)
 
 class CheckboxInput(Widget):
-    def __init__(self, attrs=None, check_test=bool):
-        super(CheckboxInput, self).__init__(attrs)
+    def __init__(self, attrs=None, row_attrs=None, check_test=bool):
+        super(CheckboxInput, self).__init__(attrs, row_attrs)
         # check_test is a callable that takes a value and returns True
         # if the checkbox should be checked for that value.
         self.check_test = check_test
@@ -201,8 +229,8 @@
         return super(CheckboxInput, self).value_from_datadict(data, files, name)
 
 class Select(Widget):
-    def __init__(self, attrs=None, choices=()):
-        super(Select, self).__init__(attrs)
+    def __init__(self, attrs=None, row_attrs=None, choices=()):
+        super(Select, self).__init__(attrs, row_attrs)
         # choices can be any iterable, but we may need to render this widget
         # multiple times. Thus, collapse it into a list so it can be consumed
         # more than once.
@@ -227,9 +255,9 @@
     """
     A Select Widget intended to be used with NullBooleanField.
     """
-    def __init__(self, attrs=None):
+    def __init__(self, attrs=None, row_attrs=None):
         choices = ((u'1', ugettext('Unknown')), (u'2', ugettext('Yes')), (u'3', ugettext('No')))
-        super(NullBooleanSelect, self).__init__(attrs, choices)
+        super(NullBooleanSelect, self).__init__(attrs, row_attrs, choices)
 
     def render(self, name, value, attrs=None, choices=()):
         try:
@@ -242,12 +270,7 @@
         value = data.get(name, None)
         return {u'2': True, u'3': False, True: True, False: False}.get(value, None)
 
-class SelectMultiple(Widget):
-    def __init__(self, attrs=None, choices=()):
-        super(SelectMultiple, self).__init__(attrs)
-        # choices can be any iterable
-        self.choices = choices
-
+class SelectMultiple(Select):
     def render(self, name, value, attrs=None, choices=()):
         if value is None: value = []
         final_attrs = self.build_attrs(attrs, name=name)
@@ -406,9 +429,9 @@
 
     You'll probably want to use this class with MultiValueField.
     """
-    def __init__(self, widgets, attrs=None):
+    def __init__(self, widgets, attrs=None, row_attrs=None):
         self.widgets = [isinstance(w, type) and w() or w for w in widgets]
-        super(MultiWidget, self).__init__(attrs)
+        super(MultiWidget, self).__init__(attrs, row_attrs)
 
     def render(self, name, value, attrs=None):
         # value is a list of values, each corresponding to a widget
@@ -460,9 +483,9 @@
     """
     A Widget that splits datetime input into two <input type="text"> boxes.
     """
-    def __init__(self, attrs=None):
+    def __init__(self, attrs=None, row_attrs=None):
         widgets = (TextInput(attrs=attrs), TextInput(attrs=attrs))
-        super(SplitDateTimeWidget, self).__init__(widgets, attrs)
+        super(SplitDateTimeWidget, self).__init__(widgets, attrs, row_attrs)
 
     def decompress(self, value):
         if value:

=== modified file 'django/utils/datastructures.py'
--- django/utils/datastructures.py	2008-02-20 07:19:29 +0000
+++ django/utils/datastructures.py	2008-02-20 07:21:00 +0000
@@ -339,3 +339,31 @@
             d = dict(self, content='<omitted>')
             return dict.__repr__(d)
         return dict.__repr__(self)
+
+class InheritableOptions(object):
+    """
+    A structure for meta attributes. Each subclass must have _defined_options
+    with names and default values.
+    """
+    def __init__(self, cls):
+        # Start with default options.
+        self.__dict__.update(self._default_options)
+        # Add options defined in bases - loop over them in *reverse*.
+        for base in cls.__mro__[::-1]:
+            try:
+                self.__dict__.update(base._meta._defined_options)
+            except AttributeError:
+                pass
+        # Add options defined here.
+        self._defined_options = {}
+        for name in self._default_options:
+            try:
+                option = getattr(cls.Meta, name)
+                # Use function if is available.
+                try:
+                    self._defined_options[name] = option.im_func
+                except AttributeError:
+                    self._defined_options[name] = option 
+            except AttributeError:
+                pass
+        self.__dict__.update(self._defined_options)

=== modified file 'tests/modeltests/model_forms/models.py'
--- tests/modeltests/model_forms/models.py	2008-02-16 07:59:58 +0000
+++ tests/modeltests/model_forms/models.py	2008-02-18 14:56:41 +0000
@@ -144,7 +144,7 @@
 ...         exclude = ['url']
 
 >>> CategoryForm.base_fields.keys()
-['name']
+['name', 'url']
 
 Don't allow more than one 'model' definition in the inheritance hierarchy.
 Technically, it would generate a valid form, but the fact that the resulting
@@ -194,7 +194,7 @@
 ...     class Meta:
 ...         model = Category
 >>> class SubCategoryForm(CategoryForm):
-...     class Meta(CategoryForm.Meta):
+...     class Meta:
 ...         exclude = ['url']
 
 >>> print SubCategoryForm()

=== modified file 'tests/regressiontests/forms/extra.py'
--- tests/regressiontests/forms/extra.py	2008-01-28 15:22:03 +0000
+++ tests/regressiontests/forms/extra.py	2008-02-07 15:23:04 +0000
@@ -372,10 +372,8 @@
 >>> f = CommentForm(data, auto_id=False, error_class=DivErrorList)
 >>> print f.as_p()
 <p>Name: <input type="text" name="name" maxlength="50" /></p>
-<div class="errorlist"><div class="error">Enter a valid e-mail address.</div></div>
-<p>Email: <input type="text" name="email" value="invalid" /></p>
-<div class="errorlist"><div class="error">This field is required.</div></div>
-<p>Comment: <input type="text" name="comment" /></p>
+<div class="errorlist"><div class="error">Enter a valid e-mail address.</div></div><p>Email: <input type="text" name="email" value="invalid" /></p>
+<div class="errorlist"><div class="error">This field is required.</div></div><p>Comment: <input type="text" name="comment" /></p>
 
 #################################
 # Test multipart-encoded form #

=== modified file 'tests/regressiontests/forms/forms.py'
--- tests/regressiontests/forms/forms.py	2008-01-28 15:22:03 +0000
+++ tests/regressiontests/forms/forms.py	2008-02-07 15:33:37 +0000
@@ -89,12 +89,9 @@
 <li><ul class="errorlist"><li>This field is required.</li></ul><label for="id_last_name">Last name:</label> <input type="text" name="last_name" id="id_last_name" /></li>
 <li><ul class="errorlist"><li>This field is required.</li></ul><label for="id_birthday">Birthday:</label> <input type="text" name="birthday" id="id_birthday" /></li>
 >>> print p.as_p()
-<ul class="errorlist"><li>This field is required.</li></ul>
-<p><label for="id_first_name">First name:</label> <input type="text" name="first_name" id="id_first_name" /></p>
-<ul class="errorlist"><li>This field is required.</li></ul>
-<p><label for="id_last_name">Last name:</label> <input type="text" name="last_name" id="id_last_name" /></p>
-<ul class="errorlist"><li>This field is required.</li></ul>
-<p><label for="id_birthday">Birthday:</label> <input type="text" name="birthday" id="id_birthday" /></p>
+<ul class="errorlist"><li>This field is required.</li></ul><p><label for="id_first_name">First name:</label> <input type="text" name="first_name" id="id_first_name" /></p>
+<ul class="errorlist"><li>This field is required.</li></ul><p><label for="id_last_name">Last name:</label> <input type="text" name="last_name" id="id_last_name" /></p>
+<ul class="errorlist"><li>This field is required.</li></ul><p><label for="id_birthday">Birthday:</label> <input type="text" name="birthday" id="id_birthday" /></p>
 
 If you don't pass any values to the Form's __init__(), or if you pass None,
 the Form will be considered unbound and won't do any validation. Form.errors
@@ -543,7 +540,8 @@
 ...     composers = MultipleChoiceField(choices=[('J', 'John Lennon'), ('P', 'Paul McCartney')], widget=MultipleHiddenInput)
 >>> f = SongFormHidden(MultiValueDict(dict(name=['Yesterday'], composers=['J', 'P'])), auto_id=False)
 >>> print f.as_ul()
-<li>Name: <input type="text" name="name" value="Yesterday" /><input type="hidden" name="composers" value="J" />
+<li>Name: <input type="text" name="name" value="Yesterday" /></li>
+<li><input type="hidden" name="composers" value="J" />
 <input type="hidden" name="composers" value="P" /></li>
 
 When using CheckboxSelectMultiple, the framework expects a list of input and
@@ -768,30 +766,36 @@
 >>> print p
 <tr><th>First name:</th><td><input type="text" name="first_name" /></td></tr>
 <tr><th>Last name:</th><td><input type="text" name="last_name" /></td></tr>
-<tr><th>Birthday:</th><td><input type="text" name="birthday" /><input type="hidden" name="hidden_text" /></td></tr>
+<tr><th>Birthday:</th><td><input type="text" name="birthday" /></td></tr>
+<tr><td colspan="2"><input type="hidden" name="hidden_text" /></td></tr>
 >>> print p.as_ul()
 <li>First name: <input type="text" name="first_name" /></li>
 <li>Last name: <input type="text" name="last_name" /></li>
-<li>Birthday: <input type="text" name="birthday" /><input type="hidden" name="hidden_text" /></li>
+<li>Birthday: <input type="text" name="birthday" /></li>
+<li><input type="hidden" name="hidden_text" /></li>
 >>> print p.as_p()
 <p>First name: <input type="text" name="first_name" /></p>
 <p>Last name: <input type="text" name="last_name" /></p>
-<p>Birthday: <input type="text" name="birthday" /><input type="hidden" name="hidden_text" /></p>
+<p>Birthday: <input type="text" name="birthday" /></p>
+<p><input type="hidden" name="hidden_text" /></p>
 
 With auto_id set, a HiddenInput still gets an ID, but it doesn't get a label.
 >>> p = Person(auto_id='id_%s')
 >>> print p
 <tr><th><label for="id_first_name">First name:</label></th><td><input type="text" name="first_name" id="id_first_name" /></td></tr>
 <tr><th><label for="id_last_name">Last name:</label></th><td><input type="text" name="last_name" id="id_last_name" /></td></tr>
-<tr><th><label for="id_birthday">Birthday:</label></th><td><input type="text" name="birthday" id="id_birthday" /><input type="hidden" name="hidden_text" id="id_hidden_text" /></td></tr>
+<tr><th><label for="id_birthday">Birthday:</label></th><td><input type="text" name="birthday" id="id_birthday" /></td></tr>
+<tr><td colspan="2"><input type="hidden" name="hidden_text" id="id_hidden_text" /></td></tr>
 >>> print p.as_ul()
 <li><label for="id_first_name">First name:</label> <input type="text" name="first_name" id="id_first_name" /></li>
 <li><label for="id_last_name">Last name:</label> <input type="text" name="last_name" id="id_last_name" /></li>
-<li><label for="id_birthday">Birthday:</label> <input type="text" name="birthday" id="id_birthday" /><input type="hidden" name="hidden_text" id="id_hidden_text" /></li>
+<li><label for="id_birthday">Birthday:</label> <input type="text" name="birthday" id="id_birthday" /></li>
+<li><input type="hidden" name="hidden_text" id="id_hidden_text" /></li>
 >>> print p.as_p()
 <p><label for="id_first_name">First name:</label> <input type="text" name="first_name" id="id_first_name" /></p>
 <p><label for="id_last_name">Last name:</label> <input type="text" name="last_name" id="id_last_name" /></p>
-<p><label for="id_birthday">Birthday:</label> <input type="text" name="birthday" id="id_birthday" /><input type="hidden" name="hidden_text" id="id_hidden_text" /></p>
+<p><label for="id_birthday">Birthday:</label> <input type="text" name="birthday" id="id_birthday" /></p>
+<p><input type="hidden" name="hidden_text" id="id_hidden_text" /></p>
 
 If a field with a HiddenInput has errors, the as_table() and as_ul() output
 will include the error message(s) with the text "(Hidden field [fieldname]) "
@@ -802,17 +806,20 @@
 <tr><td colspan="2"><ul class="errorlist"><li>(Hidden field hidden_text) This field is required.</li></ul></td></tr>
 <tr><th>First name:</th><td><input type="text" name="first_name" value="John" /></td></tr>
 <tr><th>Last name:</th><td><input type="text" name="last_name" value="Lennon" /></td></tr>
-<tr><th>Birthday:</th><td><input type="text" name="birthday" value="1940-10-9" /><input type="hidden" name="hidden_text" /></td></tr>
+<tr><th>Birthday:</th><td><input type="text" name="birthday" value="1940-10-9" /></td></tr>
+<tr><td colspan="2"><input type="hidden" name="hidden_text" /></td></tr>
 >>> print p.as_ul()
 <li><ul class="errorlist"><li>(Hidden field hidden_text) This field is required.</li></ul></li>
 <li>First name: <input type="text" name="first_name" value="John" /></li>
 <li>Last name: <input type="text" name="last_name" value="Lennon" /></li>
-<li>Birthday: <input type="text" name="birthday" value="1940-10-9" /><input type="hidden" name="hidden_text" /></li>
+<li>Birthday: <input type="text" name="birthday" value="1940-10-9" /></li>
+<li><input type="hidden" name="hidden_text" /></li>
 >>> print p.as_p()
 <ul class="errorlist"><li>(Hidden field hidden_text) This field is required.</li></ul>
 <p>First name: <input type="text" name="first_name" value="John" /></p>
 <p>Last name: <input type="text" name="last_name" value="Lennon" /></p>
-<p>Birthday: <input type="text" name="birthday" value="1940-10-9" /><input type="hidden" name="hidden_text" /></p>
+<p>Birthday: <input type="text" name="birthday" value="1940-10-9" /></p>
+<p><input type="hidden" name="hidden_text" /></p>
 
 A corner case: It's possible for a form to have only HiddenInputs.
 >>> class TestForm(Form):
@@ -820,11 +827,11 @@
 ...     bar = CharField(widget=HiddenInput)
 >>> p = TestForm(auto_id=False)
 >>> print p.as_table()
-<input type="hidden" name="foo" /><input type="hidden" name="bar" />
+<tr><td colspan="2"><input type="hidden" name="foo" /><input type="hidden" name="bar" /></td></tr>
 >>> print p.as_ul()
-<input type="hidden" name="foo" /><input type="hidden" name="bar" />
+<li><input type="hidden" name="foo" /><input type="hidden" name="bar" /></li>
 >>> print p.as_p()
-<input type="hidden" name="foo" /><input type="hidden" name="bar" />
+<p><input type="hidden" name="foo" /><input type="hidden" name="bar" /></p>
 
 A Form's fields are displayed in the same order in which they were defined.
 >>> class TestForm(Form):
@@ -1175,7 +1182,8 @@
 >>> p = UserRegistration(auto_id=False)
 >>> print p.as_ul()
 <li>Username: <input type="text" name="username" maxlength="10" /> e.g., user@example.com</li>
-<li>Password: <input type="password" name="password" /><input type="hidden" name="next" value="/" /></li>
+<li>Password: <input type="password" name="password" /></li>
+<li><input type="hidden" name="next" value="/" /></li>
 
 Help text can include arbitrary Unicode characters.
 >>> class UserRegistration(Form):
@@ -1219,10 +1227,10 @@
 ...     haircut_type = CharField()
 >>> b = Beatle(auto_id=False)
 >>> print b.as_ul()
+<li>Instrument: <input type="text" name="instrument" /></li>
 <li>First name: <input type="text" name="first_name" /></li>
 <li>Last name: <input type="text" name="last_name" /></li>
 <li>Birthday: <input type="text" name="birthday" /></li>
-<li>Instrument: <input type="text" name="instrument" /></li>
 <li>Haircut type: <input type="text" name="haircut_type" /></li>
 
 # Forms with prefixes #########################################################
@@ -1524,9 +1532,9 @@
 Recall from above that passing the "auto_id" argument to a Form gives each
 field an "id" attribute.
 >>> t = Template('''<form action="">
-... <p>{{ form.username.label_tag }}: {{ form.username }}</p>
-... <p>{{ form.password1.label_tag }}: {{ form.password1 }}</p>
-... <p>{{ form.password2.label_tag }}: {{ form.password2 }}</p>
+... <p>{{ form.username.label_tag }} {{ form.username }}</p>
+... <p>{{ form.password1.label_tag }} {{ form.password1 }}</p>
+... <p>{{ form.password2.label_tag }} {{ form.password2 }}</p>
 ... <input type="submit" />
 ... </form>''')
 >>> print t.render(Context({'form': UserRegistration(auto_id=False)}))
@@ -1538,18 +1546,18 @@
 </form>
 >>> print t.render(Context({'form': UserRegistration(auto_id='id_%s')}))
 <form action="">
-<p><label for="id_username">Username</label>: <input id="id_username" type="text" name="username" maxlength="10" /></p>
-<p><label for="id_password1">Password1</label>: <input type="password" name="password1" id="id_password1" /></p>
-<p><label for="id_password2">Password2</label>: <input type="password" name="password2" id="id_password2" /></p>
+<p><label for="id_username">Username:</label> <input id="id_username" type="text" name="username" maxlength="10" /></p>
+<p><label for="id_password1">Password1:</label> <input type="password" name="password1" id="id_password1" /></p>
+<p><label for="id_password2">Password2:</label> <input type="password" name="password2" id="id_password2" /></p>
 <input type="submit" />
 </form>
 
 User form.[field].help_text to output a field's help text. If the given field
 does not have help text, nothing will be output.
 >>> t = Template('''<form action="">
-... <p>{{ form.username.label_tag }}: {{ form.username }}<br />{{ form.username.help_text }}</p>
-... <p>{{ form.password1.label_tag }}: {{ form.password1 }}</p>
-... <p>{{ form.password2.label_tag }}: {{ form.password2 }}</p>
+... <p>{{ form.username.label_tag }} {{ form.username }}<br />{{ form.username.help_text }}</p>
+... <p>{{ form.password1.label_tag }} {{ form.password1 }}</p>
+... <p>{{ form.password2.label_tag }} {{ form.password2 }}</p>
 ... <input type="submit" />
 ... </form>''')
 >>> print t.render(Context({'form': UserRegistration(auto_id=False)}))
@@ -1562,15 +1570,6 @@
 >>> Template('{{ form.password1.help_text }}').render(Context({'form': UserRegistration(auto_id=False)}))
 u''
 
-The label_tag() method takes an optional attrs argument: a dictionary of HTML
-attributes to add to the <label> tag.
->>> f = UserRegistration(auto_id='id_%s')
->>> for bf in f:
-...     print bf.label_tag(attrs={'class': 'pretty'})
-<label for="id_username" class="pretty">Username</label>
-<label for="id_password1" class="pretty">Password1</label>
-<label for="id_password2" class="pretty">Password2</label>
-
 To display the errors that aren't associated with a particular field -- e.g.,
 the errors caused by Form.clean() -- use {{ form.non_field_errors }} in the
 template. If used on its own, it is displayed as a <ul> (or an empty string, if

=== modified file 'tests/regressiontests/forms/regressions.py'
--- tests/regressiontests/forms/regressions.py	2008-01-28 15:22:03 +0000
+++ tests/regressiontests/forms/regressions.py	2008-02-07 15:32:45 +0000
@@ -56,7 +56,7 @@
 >>> activate('ru')
 >>> f = SomeForm({})
 >>> f.as_p()
-u'<ul class="errorlist"><li>\u041e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0435 \u043f\u043e\u043b\u0435.</li></ul>\n<p><label for="id_somechoice_0">\xc5\xf8\xdf:</label> <ul>\n<li><label><input type="radio" id="id_somechoice_0" value="\xc5" name="somechoice" /> En tied\xe4</label></li>\n<li><label><input type="radio" id="id_somechoice_1" value="\xf8" name="somechoice" /> Mies</label></li>\n<li><label><input type="radio" id="id_somechoice_2" value="\xdf" name="somechoice" /> Nainen</label></li>\n</ul></p>'
+u'<ul class="errorlist"><li>\u041e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0435 \u043f\u043e\u043b\u0435.</li></ul><p><label for="id_somechoice_0">\xc5\xf8\xdf:</label> <ul>\n<li><label><input type="radio" id="id_somechoice_0" value="\xc5" name="somechoice" /> En tied\xe4</label></li>\n<li><label><input type="radio" id="id_somechoice_1" value="\xf8" name="somechoice" /> Mies</label></li>\n<li><label><input type="radio" id="id_somechoice_2" value="\xdf" name="somechoice" /> Nainen</label></li>\n</ul></p>'
 >>> deactivate()
 
 Deep copying translated text shouldn't raise an error

