=== added file 'django/forms/metaclassing.py'
--- django/forms/metaclassing.py	1970-01-01 00:00:00 +0000
+++ django/forms/metaclassing.py	2008-08-01 08:58:33 +0000
@@ -0,0 +1,71 @@
+"""
+Functions for form metaclasses. Their name describes their purpose.
+"""
+
+from django.core.exceptions import ImproperlyConfigured
+from django.utils.datastructures import SortedDict
+
+from fields import Field
+from widgets import media_property
+
+def create_meta(cls, attrs):
+    cls._meta = cls._options(getattr(cls, 'Meta', None))
+
+def create_declared_fields(cls, attrs):
+    fields = []
+    for name, possible_field in attrs.items():
+        if isinstance(possible_field, Field):
+            fields.append((name, possible_field))
+            delattr(cls, name)
+    fields.sort(lambda x, y: cmp(x[1].creation_counter, y[1].creation_counter))
+    cls.declared_fields = SortedDict(fields)
+
+def create_model_fields(cls, attrs):
+    formfield_callback = attrs.pop('formfield_callback', lambda f: f.formfield())
+    fields = []
+    if cls._meta.model:
+        for dbfield in cls._meta.model._meta.fields + cls._meta.model._meta.many_to_many:
+            if dbfield.editable:
+                formfield = formfield_callback(dbfield)
+                if formfield:
+                    fields.append((dbfield.name, formfield))
+    cls.model_fields = SortedDict(fields)
+
+def create_base_fields_pool_from_declared_fields(cls, attrs):
+    fields = []
+    for base in cls.__mro__[::-1]:
+        try:
+            fields += base.declared_fields.items()
+        except AttributeError:
+            pass
+    cls.base_fields_pool = SortedDict(fields)
+
+def create_base_fields_pool_from_model_fields_and_declared_fields(cls, attrs):
+    model_fields, declared_fields = [], []
+    for base in cls.__mro__[::-1]:
+        try:
+            declared_fields += base.declared_fields.items()
+            if base._meta.model:
+                model_fields = base.model_fields.items()
+        except AttributeError:
+            pass
+    cls.base_fields_pool = SortedDict(model_fields + declared_fields)
+
+def create_base_fields_from_base_fields_pool(cls, attrs):
+    if (cls._meta.fieldsets is None) + (cls._meta.fields is None) + (cls._meta.exclude is None) < 2:
+        raise ImproperlyConfigured("%s cannot have more than one option from fieldsets, fields and exclude." % cls.__name__)
+    if cls._meta.fieldsets:
+        names = []
+        for fieldset in cls._meta.fieldsets:
+            names.extend(fieldset['fields'])
+    elif cls._meta.fields:
+        names = cls._meta.fields
+    elif cls._meta.exclude:
+        names = [name for name in cls.base_fields_pool if name not in cls._meta.exclude]
+    else:
+        names = cls.base_fields_pool.keys()
+    cls.base_fields = SortedDict([(name, cls.base_fields_pool[name]) for name in names])
+
+def create_media(cls, attrs):
+    if not 'media' in attrs:
+        cls.media = media_property(cls)

=== modified file 'django/contrib/admin/validation.py'
--- django/contrib/admin/validation.py	2008-08-01 21:11:46 +0000
+++ django/contrib/admin/validation.py	2008-08-01 21:12:28 +0000
@@ -263,7 +263,7 @@
                 % (cls.__name__, label, field, model.__name__))
 
 def _check_form_field_exists(cls, model, opts, label, field):
-    if hasattr(cls.form, 'base_fields'):
+    if hasattr(cls.form, 'base_fields') and cls.form.base_fields:
         try:
             cls.form.base_fields[field]
         except KeyError:

=== modified file 'django/contrib/auth/forms.py'
--- django/contrib/auth/forms.py	2008-08-04 13:48:30 +0000
+++ django/contrib/auth/forms.py	2008-08-04 13:49:02 +0000
@@ -19,7 +19,7 @@
     
     class Meta:
         model = User
-        fields = ("username",)
+        fields = ("username", "password1", "password2")
     
     def clean_username(self):
         username = self.cleaned_data["username"]

=== modified file 'django/contrib/auth/tests/forms.py'
--- django/contrib/auth/tests/forms.py	2008-07-31 21:19:05 +0000
+++ django/contrib/auth/tests/forms.py	2008-07-31 21:27:17 +0000
@@ -42,7 +42,7 @@
 >>> form.is_valid()
 False
 >>> form["password2"].errors
-[u"The two password fields didn't match."]
+[u'The two password fields didn&#39;t match.']
 
 The success case.
 
@@ -107,7 +107,7 @@
 >>> form.is_valid()
 False
 >>> form["new_password2"].errors
-[u"The two password fields didn't match."]
+[u'The two password fields didn&#39;t match.']
 
 The success case.
 
@@ -145,7 +145,7 @@
 >>> form.is_valid()
 False
 >>> form["new_password2"].errors
-[u"The two password fields didn't match."]
+[u'The two password fields didn&#39;t match.']
 
 The success case.
 

=== modified file 'django/contrib/auth/tests/views.py'
--- django/contrib/auth/tests/views.py	2008-07-31 21:19:05 +0000
+++ django/contrib/auth/tests/views.py	2008-07-31 21:27:17 +0000
@@ -13,7 +13,7 @@
         response = self.client.get('/password_reset/')
         self.assertEquals(response.status_code, 200)
         response = self.client.post('/password_reset/', {'email': 'not_a_real_email@email.com'})
-        self.assertContains(response, "That e-mail address doesn't have an associated user account")
+        self.assertContains(response, "That e-mail address doesn&#39;t have an associated user account")
         self.assertEquals(len(mail.outbox), 0)
     
     def test_email_found(self):
@@ -84,5 +84,5 @@
         response = self.client.post(path, {'new_password1': 'anewpassword',
                                            'new_password2':' x'})
         self.assertEquals(response.status_code, 200)
-        self.assert_("The two password fields didn't match" in response.content)
+        self.assert_("The two password fields didn&#39;t match" in response.content)
 

=== modified file 'django/forms/extras/widgets.py'
--- django/forms/extras/widgets.py	2008-07-20 00:01:26 +0000
+++ django/forms/extras/widgets.py	2008-07-30 18:37:15 +0000
@@ -24,9 +24,9 @@
     day_field = '%s_day'
     year_field = '%s_year'
 
-    def __init__(self, attrs=None, years=None):
+    def __init__(self, attrs=None, years=None, row_attrs=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/forms/forms.py'
--- django/forms/forms.py	2008-07-20 00:01:26 +0000
+++ django/forms/forms.py	2008-08-01 08:58:33 +0000
@@ -4,14 +4,14 @@
 
 from copy import deepcopy
 
-from django.utils.datastructures import SortedDict
-from django.utils.html import escape
+from django.utils.html import conditional_escape
 from django.utils.encoding import StrAndUnicode, smart_unicode, force_unicode
 from django.utils.safestring import mark_safe
 
-from fields import Field, FileField
-from widgets import Media, media_property, TextInput, Textarea
+from fields import FileField
+from widgets import Media, TextInput, Textarea
 from util import flatatt, ErrorDict, ErrorList, ValidationError
+import metaclassing
 
 __all__ = ('BaseForm', 'Form')
 
@@ -22,45 +22,20 @@
     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.
-    Also integrates any additional media definitions
-    """
-    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(object):
+    def __init__(self, options=None):
+        self.fieldsets = getattr(options, 'fieldsets', None)
+        self.fields = getattr(options, 'fields', None)
+        self.exclude = getattr(options, 'exclude', None)
+
+class FormMetaclass(type):
     def __new__(cls, name, bases, attrs):
-        attrs['base_fields'] = get_declared_fields(bases, attrs)
-        new_class = super(DeclarativeFieldsMetaclass,
-                     cls).__new__(cls, name, bases, attrs)
-        if 'media' not in attrs:
-            new_class.media = media_property(new_class)
+        new_class = type.__new__(cls, name, bases, attrs)
+        metaclassing.create_meta(new_class, attrs)
+        metaclassing.create_declared_fields(new_class, attrs)
+        metaclassing.create_base_fields_pool_from_declared_fields(new_class, attrs)
+        metaclassing.create_base_fields_from_base_fields_pool(new_class, attrs)
+        metaclassing.create_media(new_class, attrs)
         return new_class
 
 class BaseForm(StrAndUnicode):
@@ -106,7 +81,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
@@ -119,70 +94,169 @@
         """
         return self.is_bound and not bool(self.errors)
 
-    def add_prefix(self, field_name):
+    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 has_fieldsets(self):
+        "Returns True if this form has fieldsets."
+        return bool(self._meta.fieldsets)
+
+    def first_fieldset_attrs(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 _label_tag_html_output(self, bf, label_tag_html):
+        "Helper function for outputting HTML from a label. Used by _row_html_output."
+        label, label_id = bf.label, bf.label_id
+        if self.label_suffix and label and label[-1] not in ':?.!':
+            label += self.label_suffix
+        if label and label_id:
+            return label_tag_html % {
+                'label': label,
+                'id': label_id,
+            }
+        else:
+            return label
+
+    def _help_text_html_output(self, bf, help_text_html):
+        "Helper function for outputting HTML from a help text. Used by _row_html_output."
+        if bf.help_text:
+            return help_text_html % {
+                'help_text': bf.help_text,
+            }
+        else:
+            return u''
+
+    def _row_html_output(self, bf, row_html, label_tag_html, help_text_html):
+        "Helper function for outputting HTML from a widget. Used by _html_output."
+        return row_html % {
+            'rendered_widget': unicode(bf),
+            'rendered_errors': unicode(bf.errors),
+            'label_tag': self._label_tag_html_output(bf, label_tag_html),
+            'help_text': self._help_text_html_output(bf, help_text_html),
+            'attrs': bf.row_attrs,
+        }
+
+    def _top_errors_html_output(self, top_errors, top_errors_html):
+        "Helper function for outputting HTML from a top errors. Used by _html_output."
+        return top_errors_html % {
+            'top_errors': unicode(top_errors),
+        }
+
+    def _fieldset_html_output(self, fields, fieldset, is_first, is_last, fieldset_start_html, fieldset_end_html, legend_tag_html):
+        "Helper function for outputting HTML from a fieldset. Used by _html_output."
+        output = []
+        if not is_first:
+            legend_tag = attrs = u''
+            if 'legend' in fieldset:
+                legend_tag = legend_tag_html % {
+                    'legend': conditional_escape(force_unicode(fieldset['legend'])),
+                }
+            if 'attrs' in fieldset:
+                attrs = flatatt(fieldset.get('attrs'))
+            output.append(fieldset_start_html % {
+                'legend_tag': legend_tag,
+                'attrs': attrs,
+            })
+        for name in fieldset['fields']:
+            output.append(fields[name])
+        if not is_last:
+            output.append(fieldset_end_html)
+        return u'\n'.join(output)
+
+    def _hidden_fields_html_output(self, hidden_fields, hidden_fields_html):
+        "Helper function for outputting HTML from a hidden fields. Used by _html_output."
+        return hidden_fields_html % {
+            'hidden_fields': u''.join(hidden_fields),
+        }
+
+    def _html_output(self, row_html, label_tag_html, help_text_html, top_errors_html,
+            fieldset_start_html, legend_tag_html, fieldset_end_html, hidden_fields_html):
         "Helper function for outputting HTML. Used by as_table(), as_ul(), as_p()."
-        top_errors = self.non_field_errors() # Errors that should be displayed above all fields.
-        output, hidden_fields = [], []
+        output = []
+        top_errors, hidden_fields, visible_fields = self.non_field_errors(), [], {}
         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([u'(Hidden field %s) %s' % (name, force_unicode(e)) for e in bf_errors])
+                if bf.errors:
+                    top_errors.extend([u'(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] = self._row_html_output(bf, row_html, label_tag_html, help_text_html)
         if top_errors:
-            output.insert(0, error_row % force_unicode(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_html))
+        if self.has_fieldsets():
+            for i, fieldset in enumerate(self._meta.fieldsets):
+                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(self._fieldset_html_output(fields, fieldset, is_first, is_last,
+                    fieldset_start_html, fieldset_end_html, legend_tag_html))
+        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_html))
         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)
+        kwargs = {
+            'row_html': u'<tr%(attrs)s><th>%(label_tag)s</th><td>%(rendered_errors)s%(rendered_widget)s%(help_text)s</td></tr>',
+            'label_tag_html': u'<label for="%(id)s">%(label)s</label>',
+            'help_text_html': u'<br />%(help_text)s',
+            'top_errors_html': u'<tr><td colspan="2">%(top_errors)s</td></tr>',
+            'fieldset_start_html': u'<fieldset%(attrs)s>\n%(legend_tag)s<table>',
+            'fieldset_end_html': u'</table>\n</fieldset>',
+            'legend_tag_html': u'<legend>%(legend)s</legend>\n',
+            'hidden_fields_html': u'<tr class="hidden"><td colspan="2">%(hidden_fields)s</td></tr>',
+        }
+        return self._html_output(**kwargs)
 
     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)
+        kwargs = {
+            'row_html': u'<li%(attrs)s>%(rendered_errors)s%(label_tag)s %(rendered_widget)s%(help_text)s</li>',
+            'label_tag_html': u'<label for="%(id)s">%(label)s</label>',
+            'help_text_html': u' %(help_text)s',
+            'top_errors_html': u'<li>%(top_errors)s</li>',
+            'fieldset_start_html': u'<fieldset%(attrs)s>\n%(legend_tag)s<ul>',
+            'fieldset_end_html': u'</ul>\n</fieldset>',
+            'legend_tag_html': u'<legend>%(legend)s</legend>\n',
+            'hidden_fields_html': u'<li class="hidden">%(hidden_fields)s</li>',
+        }
+        return self._html_output(**kwargs)
 
     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)
+        kwargs = {
+            'row_html': u'%(rendered_errors)s<p%(attrs)s>%(label_tag)s %(rendered_widget)s%(help_text)s</p>',
+            'label_tag_html': u'<label for="%(id)s">%(label)s</label>',
+            'help_text_html': u' %(help_text)s',
+            'top_errors_html': u'%(top_errors)s',
+            'fieldset_start_html': u'<fieldset%(attrs)s>\n%(legend_tag)s',
+            'fieldset_end_html': u'</fieldset>',
+            'legend_tag_html': u'<legend>%(legend)s</legend>\n',
+            'hidden_fields_html': u'<p class="hidden">%(hidden_fields)s</p>',
+        }
+        return self._html_output(**kwargs)
 
     def non_field_errors(self):
         """
@@ -221,13 +295,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')
 
@@ -291,20 +365,19 @@
     # 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.required = self.field.required
+        self.is_hidden = self.widget.is_hidden
         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."""
@@ -325,7 +398,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:
@@ -358,9 +431,37 @@
         """
         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(self):
+        "Returns label for this field as safe HTML."
+        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_id(self):
+        "Returns label id for this field as safe HTML."
+        id_ = self.widget.attrs.get('id') or self.auto_id
+        if id_:
+            return self.widget.id_for_label(id_)
+    label_id = property(_label_id)
+
+    def _help_text(self):
+        "Returns help text for this field as safe HTML."
+        if self.field.help_text is None:
+            return u''
+        else:
+            return force_unicode(self.field.help_text)
+    help_text = property(_help_text)
+
+    def _row_attrs(self):
+        "Returns row attributes for this field as safe HTML."
+        return flatatt(self.widget.row_attrs)
+    row_attrs = property(_row_attrs)
+
     def label_tag(self, contents=None, attrs=None):
         """
         Wraps the given contents in a <label>, if the field has an ID attribute.
@@ -369,19 +470,12 @@
 
         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_:
+        contents = contents or self.label
+        if self.label_id:
             attrs = attrs and flatatt(attrs) or ''
-            contents = '<label for="%s"%s>%s</label>' % (widget.id_for_label(id_), attrs, contents)
+            contents = '<label for="%s"%s>%s</label>' % (self.label_id, attrs, contents)
         return mark_safe(contents)
 
-    def _is_hidden(self):
-        "Returns True if this BoundField's widget is hidden."
-        return self.field.widget.is_hidden
-    is_hidden = property(_is_hidden)
-
     def _auto_id(self):
         """
         Calculates and returns the ID attribute for this BoundField, if the

=== modified file 'django/forms/models.py'
--- django/forms/models.py	2008-08-01 21:11:46 +0000
+++ django/forms/models.py	2008-08-01 21:12:28 +0000
@@ -10,11 +10,11 @@
 from django.utils.datastructures import SortedDict
 
 from util import ValidationError, ErrorList
-from forms import BaseForm, get_declared_fields
+from forms import FormOptions, FormMetaclass, BaseForm
 from fields import Field, ChoiceField, IntegerField, EMPTY_VALUES
 from widgets import Select, SelectMultiple, HiddenInput, MultipleHiddenInput
-from widgets import media_property
 from formsets import BaseFormSet, formset_factory, DELETION_FIELD_NAME
+import metaclassing
 
 __all__ = (
     'ModelForm', 'BaseModelForm', 'model_to_dict', 'fields_for_model',
@@ -206,42 +206,20 @@
             field_list.append((f.name, formfield))
     return SortedDict(field_list)
 
-class ModelFormOptions(object):
+class ModelFormOptions(FormOptions):
     def __init__(self, options=None):
+        super(ModelFormOptions, self).__init__(options)
         self.model = getattr(options, 'model', None)
-        self.fields = getattr(options, 'fields', None)
-        self.exclude = getattr(options, 'exclude', None)
-
-
-class ModelFormMetaclass(type):
+
+class ModelFormMetaclass(FormMetaclass):
     def __new__(cls, name, bases, attrs):
-        formfield_callback = attrs.pop('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
-        new_class = super(ModelFormMetaclass, cls).__new__(cls, name, bases,
-                attrs)
-        if not parents:
-            return new_class
-
-        if 'media' not in attrs:
-            new_class.media = media_property(new_class)
-        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
+        new_class = type.__new__(cls, name, bases, attrs)
+        metaclassing.create_meta(new_class, attrs)
+        metaclassing.create_model_fields(new_class, attrs)
+        metaclassing.create_declared_fields(new_class, attrs)
+        metaclassing.create_base_fields_pool_from_model_fields_and_declared_fields(new_class, attrs)
+        metaclassing.create_base_fields_from_base_fields_pool(new_class, attrs)
+        metaclassing.create_media(new_class, attrs)
         return new_class
 
 class BaseModelForm(BaseForm):
@@ -255,7 +233,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)
@@ -274,10 +252,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
 
 def modelform_factory(model, form=ModelForm, fields=None, exclude=None,
                        formfield_callback=lambda f: f.formfield()):

=== modified file 'django/forms/util.py'
--- django/forms/util.py	2008-07-24 12:03:58 +0000
+++ django/forms/util.py	2008-07-30 18:37:15 +0000
@@ -1,4 +1,4 @@
-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.safestring import mark_safe
 
@@ -9,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):
     """
@@ -55,9 +55,9 @@
         a string) or a list of objects.
         """
         if isinstance(message, list):
-            self.messages = ErrorList([smart_unicode(msg) for msg in message])
+            self.messages = ErrorList([conditional_escape(smart_unicode(msg)) for msg in message])
         else:
-            message = smart_unicode(message)
+            message = conditional_escape(smart_unicode(message))
             self.messages = ErrorList([message])
 
     def __str__(self):

=== modified file 'django/forms/widgets.py'
--- django/forms/widgets.py	2008-07-20 00:01:25 +0000
+++ django/forms/widgets.py	2008-07-30 18:37:15 +0000
@@ -132,15 +132,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
 
@@ -220,8 +225,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, render_value=True, row_attrs=None):
+        super(PasswordInput, self).__init__(attrs, row_attrs)
         self.render_value = render_value
 
     def render(self, name, value, attrs=None):
@@ -237,8 +242,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, choices=(), row_attrs=None):
+        super(MultipleHiddenInput, self).__init__(attrs, row_attrs)
         # choices can be any iterable
         self.choices = choices
 
@@ -271,11 +276,12 @@
         return True
 
 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 = ''
@@ -288,8 +294,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, format=None, row_attrs=None):
+        super(DateTimeInput, self).__init__(attrs, row_attrs)
         if format:
             self.format = format
 
@@ -302,8 +308,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, check_test=bool, row_attrs=None):
+        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
@@ -334,8 +340,8 @@
         return bool(initial) != bool(data)
 
 class Select(Widget):
-    def __init__(self, attrs=None, choices=()):
-        super(Select, self).__init__(attrs)
+    def __init__(self, attrs=None, choices=(), row_attrs=None):
+        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.
@@ -375,9 +381,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, choices, row_attrs)
 
     def render(self, name, value, attrs=None, choices=()):
         try:
@@ -570,9 +576,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
@@ -642,9 +648,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 'tests/modeltests/model_forms/models.py'
--- tests/modeltests/model_forms/models.py	2008-07-21 12:42:18 +0000
+++ tests/modeltests/model_forms/models.py	2008-07-30 18:37:15 +0000
@@ -151,9 +151,16 @@
 ...         model = Category
 ...         fields = ['name', 'url']
 ...         exclude = ['url']
-
->>> CategoryForm.base_fields.keys()
-['name']
+Traceback (most recent call last):
+  File "/home/petr/django/local2/00-forms-fieldsets/django/test/_doctest.py", line 1267, in __run
+    compileflags, 1) in test.globs
+  File "<doctest modeltests.model_forms.models.__test__.API_TESTS[12]>", line 1, in ?
+    class CategoryForm(ModelForm):
+  File "/home/petr/django/local2/00-forms-fieldsets/django/forms/models.py", line 220, in __new__
+    metaclassing.create_base_fields_from_base_fields_pool(new_class)
+  File "/home/petr/django/local2/00-forms-fieldsets/django/forms/metaclassing.py", line 50, in create_base_fields_from_base_fields_pool
+    raise ImproperlyConfigured("%s cannot have more than one option from fieldsets, fields and exclude." % cls.__name__)
+ImproperlyConfigured: CategoryForm cannot have more than one option from fieldsets, fields and exclude.
 
 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

=== modified file 'tests/modeltests/model_formsets/models.py'
--- tests/modeltests/model_formsets/models.py	2008-08-01 21:11:46 +0000
+++ tests/modeltests/model_formsets/models.py	2008-08-01 21:12:28 +0000
@@ -46,9 +46,12 @@
 >>> formset = AuthorFormSet(queryset=qs)
 >>> for form in formset.forms:
 ...     print form.as_p()
-<p><label for="id_form-0-name">Name:</label> <input id="id_form-0-name" type="text" name="form-0-name" maxlength="100" /><input type="hidden" name="form-0-id" id="id_form-0-id" /></p>
-<p><label for="id_form-1-name">Name:</label> <input id="id_form-1-name" type="text" name="form-1-name" maxlength="100" /><input type="hidden" name="form-1-id" id="id_form-1-id" /></p>
-<p><label for="id_form-2-name">Name:</label> <input id="id_form-2-name" type="text" name="form-2-name" maxlength="100" /><input type="hidden" name="form-2-id" id="id_form-2-id" /></p>
+<p><label for="id_form-0-name">Name:</label> <input id="id_form-0-name" type="text" name="form-0-name" maxlength="100" /></p>
+<p class ="hidden"><input type="hidden" name="form-0-id" id="id_form-0-id" /></p>
+<p><label for="id_form-1-name">Name:</label> <input id="id_form-1-name" type="text" name="form-1-name" maxlength="100" /></p>
+<p class ="hidden"><input type="hidden" name="form-1-id" id="id_form-1-id" /></p>
+<p><label for="id_form-2-name">Name:</label> <input id="id_form-2-name" type="text" name="form-2-name" maxlength="100" /></p>
+<p class ="hidden"><input type="hidden" name="form-2-id" id="id_form-2-id" /></p>
 
 >>> data = {
 ...     'form-TOTAL_FORMS': '3', # the number of forms rendered
@@ -82,9 +85,12 @@
 >>> formset = AuthorFormSet(queryset=qs)
 >>> for form in formset.forms:
 ...     print form.as_p()
-<p><label for="id_form-0-name">Name:</label> <input id="id_form-0-name" type="text" name="form-0-name" value="Arthur Rimbaud" maxlength="100" /><input type="hidden" name="form-0-id" value="2" id="id_form-0-id" /></p>
-<p><label for="id_form-1-name">Name:</label> <input id="id_form-1-name" type="text" name="form-1-name" value="Charles Baudelaire" maxlength="100" /><input type="hidden" name="form-1-id" value="1" id="id_form-1-id" /></p>
-<p><label for="id_form-2-name">Name:</label> <input id="id_form-2-name" type="text" name="form-2-name" maxlength="100" /><input type="hidden" name="form-2-id" id="id_form-2-id" /></p>
+<p><label for="id_form-0-name">Name:</label> <input id="id_form-0-name" type="text" name="form-0-name" value="Arthur Rimbaud" maxlength="100" /></p>
+<p class ="hidden"><input type="hidden" name="form-0-id" value="2" id="id_form-0-id" /></p>
+<p><label for="id_form-1-name">Name:</label> <input id="id_form-1-name" type="text" name="form-1-name" value="Charles Baudelaire" maxlength="100" /></p>
+<p class ="hidden"><input type="hidden" name="form-1-id" value="1" id="id_form-1-id" /></p>
+<p><label for="id_form-2-name">Name:</label> <input id="id_form-2-name" type="text" name="form-2-name" maxlength="100" /></p>
+<p class ="hidden"><input type="hidden" name="form-2-id" id="id_form-2-id" /></p>
 
 
 >>> data = {
@@ -122,13 +128,17 @@
 >>> for form in formset.forms:
 ...     print form.as_p()
 <p><label for="id_form-0-name">Name:</label> <input id="id_form-0-name" type="text" name="form-0-name" value="Arthur Rimbaud" maxlength="100" /></p>
-<p><label for="id_form-0-DELETE">Delete:</label> <input type="checkbox" name="form-0-DELETE" id="id_form-0-DELETE" /><input type="hidden" name="form-0-id" value="2" id="id_form-0-id" /></p>
+<p><label for="id_form-0-DELETE">Delete:</label> <input type="checkbox" name="form-0-DELETE" id="id_form-0-DELETE" /></p>
+<p class ="hidden"><input type="hidden" name="form-0-id" value="2" id="id_form-0-id" /></p>
 <p><label for="id_form-1-name">Name:</label> <input id="id_form-1-name" type="text" name="form-1-name" value="Charles Baudelaire" maxlength="100" /></p>
-<p><label for="id_form-1-DELETE">Delete:</label> <input type="checkbox" name="form-1-DELETE" id="id_form-1-DELETE" /><input type="hidden" name="form-1-id" value="1" id="id_form-1-id" /></p>
+<p><label for="id_form-1-DELETE">Delete:</label> <input type="checkbox" name="form-1-DELETE" id="id_form-1-DELETE" /></p>
+<p class ="hidden"><input type="hidden" name="form-1-id" value="1" id="id_form-1-id" /></p>
 <p><label for="id_form-2-name">Name:</label> <input id="id_form-2-name" type="text" name="form-2-name" value="Paul Verlaine" maxlength="100" /></p>
-<p><label for="id_form-2-DELETE">Delete:</label> <input type="checkbox" name="form-2-DELETE" id="id_form-2-DELETE" /><input type="hidden" name="form-2-id" value="3" id="id_form-2-id" /></p>
+<p><label for="id_form-2-DELETE">Delete:</label> <input type="checkbox" name="form-2-DELETE" id="id_form-2-DELETE" /></p>
+<p class ="hidden"><input type="hidden" name="form-2-id" value="3" id="id_form-2-id" /></p>
 <p><label for="id_form-3-name">Name:</label> <input id="id_form-3-name" type="text" name="form-3-name" maxlength="100" /></p>
-<p><label for="id_form-3-DELETE">Delete:</label> <input type="checkbox" name="form-3-DELETE" id="id_form-3-DELETE" /><input type="hidden" name="form-3-id" id="id_form-3-id" /></p>
+<p><label for="id_form-3-DELETE">Delete:</label> <input type="checkbox" name="form-3-DELETE" id="id_form-3-DELETE" /></p>
+<p class ="hidden"><input type="hidden" name="form-3-id" id="id_form-3-id" /></p>
 
 >>> data = {
 ...     'form-TOTAL_FORMS': '4', # the number of forms rendered
@@ -243,9 +253,12 @@
 >>> formset = AuthorBooksFormSet(instance=author)
 >>> for form in formset.forms:
 ...     print form.as_p()
-<p><label for="id_book_set-0-title">Title:</label> <input id="id_book_set-0-title" type="text" name="book_set-0-title" maxlength="100" /><input type="hidden" name="book_set-0-id" id="id_book_set-0-id" /></p>
-<p><label for="id_book_set-1-title">Title:</label> <input id="id_book_set-1-title" type="text" name="book_set-1-title" maxlength="100" /><input type="hidden" name="book_set-1-id" id="id_book_set-1-id" /></p>
-<p><label for="id_book_set-2-title">Title:</label> <input id="id_book_set-2-title" type="text" name="book_set-2-title" maxlength="100" /><input type="hidden" name="book_set-2-id" id="id_book_set-2-id" /></p>
+<p><label for="id_book_set-0-title">Title:</label> <input id="id_book_set-0-title" type="text" name="book_set-0-title" maxlength="100" /></p>
+<p class ="hidden"><input type="hidden" name="book_set-0-id" id="id_book_set-0-id" /></p>
+<p><label for="id_book_set-1-title">Title:</label> <input id="id_book_set-1-title" type="text" name="book_set-1-title" maxlength="100" /></p>
+<p class ="hidden"><input type="hidden" name="book_set-1-id" id="id_book_set-1-id" /></p>
+<p><label for="id_book_set-2-title">Title:</label> <input id="id_book_set-2-title" type="text" name="book_set-2-title" maxlength="100" /></p>
+<p class ="hidden"><input type="hidden" name="book_set-2-id" id="id_book_set-2-id" /></p>
 
 >>> data = {
 ...     'book_set-TOTAL_FORMS': '3', # the number of forms rendered
@@ -277,9 +290,12 @@
 >>> formset = AuthorBooksFormSet(instance=author)
 >>> for form in formset.forms:
 ...     print form.as_p()
-<p><label for="id_book_set-0-title">Title:</label> <input id="id_book_set-0-title" type="text" name="book_set-0-title" value="Les Fleurs du Mal" maxlength="100" /><input type="hidden" name="book_set-0-id" value="1" id="id_book_set-0-id" /></p>
-<p><label for="id_book_set-1-title">Title:</label> <input id="id_book_set-1-title" type="text" name="book_set-1-title" maxlength="100" /><input type="hidden" name="book_set-1-id" id="id_book_set-1-id" /></p>
-<p><label for="id_book_set-2-title">Title:</label> <input id="id_book_set-2-title" type="text" name="book_set-2-title" maxlength="100" /><input type="hidden" name="book_set-2-id" id="id_book_set-2-id" /></p>
+<p><label for="id_book_set-0-title">Title:</label> <input id="id_book_set-0-title" type="text" name="book_set-0-title" value="Les Fleurs du Mal" maxlength="100" /></p>
+<p class ="hidden"><input type="hidden" name="book_set-0-id" value="1" id="id_book_set-0-id" /></p>
+<p><label for="id_book_set-1-title">Title:</label> <input id="id_book_set-1-title" type="text" name="book_set-1-title" maxlength="100" /></p>
+<p class ="hidden"><input type="hidden" name="book_set-1-id" id="id_book_set-1-id" /></p>
+<p><label for="id_book_set-2-title">Title:</label> <input id="id_book_set-2-title" type="text" name="book_set-2-title" maxlength="100" /></p>
+<p class ="hidden"><input type="hidden" name="book_set-2-id" id="id_book_set-2-id" /></p>
 
 >>> data = {
 ...     'book_set-TOTAL_FORMS': '3', # the number of forms rendered
@@ -331,8 +347,10 @@
 >>> formset = AuthorBooksFormSet(prefix="test")
 >>> for form in formset.forms:
 ...     print form.as_p()
-<p><label for="id_test-0-title">Title:</label> <input id="id_test-0-title" type="text" name="test-0-title" maxlength="100" /><input type="hidden" name="test-0-id" id="id_test-0-id" /></p>
-<p><label for="id_test-1-title">Title:</label> <input id="id_test-1-title" type="text" name="test-1-title" maxlength="100" /><input type="hidden" name="test-1-id" id="id_test-1-id" /></p>
+<p><label for="id_test-0-title">Title:</label> <input id="id_test-0-title" type="text" name="test-0-title" maxlength="100" /></p>
+<p class ="hidden"><input type="hidden" name="test-0-id" id="id_test-0-id" /></p>
+<p><label for="id_test-1-title">Title:</label> <input id="id_test-1-title" type="text" name="test-1-title" maxlength="100" /></p>
+<p class ="hidden"><input type="hidden" name="test-1-id" id="id_test-1-id" /></p>
 
 # Test a custom primary key ###################################################
 

=== modified file 'tests/regressiontests/forms/extra.py'
--- tests/regressiontests/forms/extra.py	2008-07-20 00:01:26 +0000
+++ tests/regressiontests/forms/extra.py	2008-07-20 20:54:46 +0000
@@ -439,10 +439,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-07-20 00:01:26 +0000
+++ tests/regressiontests/forms/forms.py	2008-07-30 18:37:15 +0000
@@ -94,12 +94,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
@@ -563,7 +560,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 class ="hidden"><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
@@ -808,30 +806,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 class="hidden"><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 class ="hidden"><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 class ="hidden"><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 class="hidden"><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 class ="hidden"><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 class ="hidden"><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]) "
@@ -842,17 +846,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 class="hidden"><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 class ="hidden"><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 class ="hidden"><input type="hidden" name="hidden_text" /></p>
 
 A corner case: It's possible for a form to have only HiddenInputs.
 >>> class TestForm(Form):
@@ -860,11 +867,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 class="hidden"><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 class ="hidden"><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 class ="hidden"><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):
@@ -1215,7 +1222,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 class ="hidden"><input type="hidden" name="next" value="/" /></li>
 
 Help text can include arbitrary Unicode characters.
 >>> class UserRegistration(Form):
@@ -1259,10 +1267,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 #########################################################

=== modified file 'tests/regressiontests/forms/formsets.py'
--- tests/regressiontests/forms/formsets.py	2008-07-24 12:03:58 +0000
+++ tests/regressiontests/forms/formsets.py	2008-07-30 18:37:15 +0000
@@ -20,7 +20,7 @@
 
 >>> formset = ChoiceFormSet(auto_id=False, prefix='choices')
 >>> print formset
-<input type="hidden" name="choices-TOTAL_FORMS" value="1" /><input type="hidden" name="choices-INITIAL_FORMS" value="0" />
+<tr class="hidden"><td colspan="2"><input type="hidden" name="choices-TOTAL_FORMS" value="1" /><input type="hidden" name="choices-INITIAL_FORMS" value="0" /></td></tr>
 <tr><th>Choice:</th><td><input type="text" name="choices-0-choice" /></td></tr>
 <tr><th>Votes:</th><td><input type="text" name="choices-0-votes" /></td></tr>
 

=== modified file 'tests/regressiontests/forms/localflavor/ch.py'
--- tests/regressiontests/forms/localflavor/ch.py	2007-11-13 02:22:36 +0000
+++ tests/regressiontests/forms/localflavor/ch.py	2008-07-20 21:27:55 +0000
@@ -41,13 +41,13 @@
 >>> f.clean('C1234567<1')
 Traceback (most recent call last):
 ...
-ValidationError: [u'Enter a valid Swiss identity or passport card number in X1234567<0 or 1234567890 format.']
+ValidationError: [u'Enter a valid Swiss identity or passport card number in X1234567&lt;0 or 1234567890 format.']
 >>> f.clean('2123456700')
 u'2123456700'
 >>> f.clean('2123456701')
 Traceback (most recent call last):
 ...
-ValidationError: [u'Enter a valid Swiss identity or passport card number in X1234567<0 or 1234567890 format.']
+ValidationError: [u'Enter a valid Swiss identity or passport card number in X1234567&lt;0 or 1234567890 format.']
 
 # CHStateSelect #############################################################
 

=== modified file 'tests/regressiontests/forms/regressions.py'
--- tests/regressiontests/forms/regressions.py	2008-07-20 00:01:26 +0000
+++ tests/regressiontests/forms/regressions.py	2008-07-20 21:29:49 +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 for="id_somechoice_0"><input type="radio" id="id_somechoice_0" value="\xc5" name="somechoice" /> En tied\xe4</label></li>\n<li><label for="id_somechoice_1"><input type="radio" id="id_somechoice_1" value="\xf8" name="somechoice" /> Mies</label></li>\n<li><label for="id_somechoice_2"><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 for="id_somechoice_0"><input type="radio" id="id_somechoice_0" value="\xc5" name="somechoice" /> En tied\xe4</label></li>\n<li><label for="id_somechoice_1"><input type="radio" id="id_somechoice_1" value="\xf8" name="somechoice" /> Mies</label></li>\n<li><label for="id_somechoice_2"><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

=== modified file 'tests/regressiontests/forms/util.py'
--- tests/regressiontests/forms/util.py	2008-07-20 00:01:26 +0000
+++ tests/regressiontests/forms/util.py	2008-07-31 21:27:18 +0000
@@ -7,6 +7,11 @@
 >>> from django.forms.util import *
 >>> from django.utils.translation import ugettext_lazy
 
+# Escaping.
+>>> from django.utils.html import escape
+>>> from django.utils.html import conditional_escape
+>>> script = "$('#example').html('<a href=\"http://www.example.com/\">example</a>');"
+
 ###########
 # flatatt #
 ###########
@@ -19,6 +24,22 @@
 >>> flatatt({})
 u''
 
+# Escaping.
+
+>>> flatatt({'onclick': script})
+u' onclick="$(&#39;#example&#39;).html(&#39;&lt;a href=&quot;http://www.example.com/&quot;&gt;example&lt;/a&gt;&#39;);"'
+>>> flatatt({'onclick': escape(script)})
+u' onclick="$(&#39;#example&#39;).html(&#39;&lt;a href=&quot;http://www.example.com/&quot;&gt;example&lt;/a&gt;&#39;);"'
+>>> flatatt({'onclick': conditional_escape(script)})
+u' onclick="$(&#39;#example&#39;).html(&#39;&lt;a href=&quot;http://www.example.com/&quot;&gt;example&lt;/a&gt;&#39;);"'
+
+>>> conditional_escape(flatatt({'onclick': script}))
+u' onclick="$(&#39;#example&#39;).html(&#39;&lt;a href=&quot;http://www.example.com/&quot;&gt;example&lt;/a&gt;&#39;);"'
+>>> conditional_escape(flatatt({'onclick': escape(script)}))
+u' onclick="$(&#39;#example&#39;).html(&#39;&lt;a href=&quot;http://www.example.com/&quot;&gt;example&lt;/a&gt;&#39;);"'
+>>> conditional_escape(flatatt({'onclick': conditional_escape(script)}))
+u' onclick="$(&#39;#example&#39;).html(&#39;&lt;a href=&quot;http://www.example.com/&quot;&gt;example&lt;/a&gt;&#39;);"'
+
 ###################
 # ValidationError #
 ###################
@@ -49,4 +70,33 @@
 # Can take a non-string.
 >>> print ValidationError(VeryBadError()).messages
 <ul class="errorlist"><li>A very bad error.</li></ul>
+
+# Escaping.
+
+>>> print ValidationError(script).messages
+<ul class="errorlist"><li>$(&#39;#example&#39;).html(&#39;&lt;a href=&quot;http://www.example.com/&quot;&gt;example&lt;/a&gt;&#39;);</li></ul>
+>>> print ValidationError(escape(script)).messages
+<ul class="errorlist"><li>$(&#39;#example&#39;).html(&#39;&lt;a href=&quot;http://www.example.com/&quot;&gt;example&lt;/a&gt;&#39;);</li></ul>
+>>> print ValidationError(conditional_escape(script)).messages
+<ul class="errorlist"><li>$(&#39;#example&#39;).html(&#39;&lt;a href=&quot;http://www.example.com/&quot;&gt;example&lt;/a&gt;&#39;);</li></ul>
+>>> print ErrorDict({'example': ValidationError(script).messages})
+<ul class="errorlist"><li>example<ul class="errorlist"><li>$(&#39;#example&#39;).html(&#39;&lt;a href=&quot;http://www.example.com/&quot;&gt;example&lt;/a&gt;&#39;);</li></ul></li></ul>
+>>> print ErrorDict({'example': ValidationError(escape(script)).messages})
+<ul class="errorlist"><li>example<ul class="errorlist"><li>$(&#39;#example&#39;).html(&#39;&lt;a href=&quot;http://www.example.com/&quot;&gt;example&lt;/a&gt;&#39;);</li></ul></li></ul>
+>>> print ErrorDict({'example': ValidationError(conditional_escape(script)).messages})
+<ul class="errorlist"><li>example<ul class="errorlist"><li>$(&#39;#example&#39;).html(&#39;&lt;a href=&quot;http://www.example.com/&quot;&gt;example&lt;/a&gt;&#39;);</li></ul></li></ul>
+
+>>> print conditional_escape(unicode(ValidationError(script).messages))
+<ul class="errorlist"><li>$(&#39;#example&#39;).html(&#39;&lt;a href=&quot;http://www.example.com/&quot;&gt;example&lt;/a&gt;&#39;);</li></ul>
+>>> print conditional_escape(unicode(ValidationError(escape(script)).messages))
+<ul class="errorlist"><li>$(&#39;#example&#39;).html(&#39;&lt;a href=&quot;http://www.example.com/&quot;&gt;example&lt;/a&gt;&#39;);</li></ul>
+>>> print conditional_escape(unicode(ValidationError(conditional_escape(script)).messages))
+<ul class="errorlist"><li>$(&#39;#example&#39;).html(&#39;&lt;a href=&quot;http://www.example.com/&quot;&gt;example&lt;/a&gt;&#39;);</li></ul>
+>>> print conditional_escape(unicode(ErrorDict({'example': ValidationError(script).messages})))
+<ul class="errorlist"><li>example<ul class="errorlist"><li>$(&#39;#example&#39;).html(&#39;&lt;a href=&quot;http://www.example.com/&quot;&gt;example&lt;/a&gt;&#39;);</li></ul></li></ul>
+>>> print conditional_escape(unicode(ErrorDict({'example': ValidationError(escape(script)).messages})))
+<ul class="errorlist"><li>example<ul class="errorlist"><li>$(&#39;#example&#39;).html(&#39;&lt;a href=&quot;http://www.example.com/&quot;&gt;example&lt;/a&gt;&#39;);</li></ul></li></ul>
+>>> print conditional_escape(unicode(ErrorDict({'example': ValidationError(conditional_escape(script)).messages})))
+<ul class="errorlist"><li>example<ul class="errorlist"><li>$(&#39;#example&#39;).html(&#39;&lt;a href=&quot;http://www.example.com/&quot;&gt;example&lt;/a&gt;&#39;);</li></ul></li></ul>
+
 """

