Ticket #3025: newforms_autoid.patch

File newforms_autoid.patch, 1.3 KB (added by Chris Beaven, 18 years ago)
  • django/newforms/forms.py

     
    2222class Form(object):
    2323    "A collection of Fields, plus their associated data."
    2424    __metaclass__ = DeclarativeFieldsMetaclass
     25   
     26    auto_id = None
    2527
    2628    def __init__(self, data=None): # TODO: prefix stuff
    2729        self.data = data or {}
     
    156158    errors = property(_errors)
    157159
    158160    def as_widget(self, widget, attrs=None):
     161        attrs = attrs or {}
     162        if attrs and not attrs.has_key('id') and self.auto_id:
     163            attrs['id'] = self.auto_id
    159164        return widget.render(self._name, self._form.data.get(self._name, None), attrs=attrs)
    160165
    161166    def as_text(self, attrs=None):
     
    167172    def as_textarea(self, attrs=None):
    168173        "Returns a string of HTML for representing this as a <textarea>."
    169174        return self.as_widget(Textarea(), attrs)
     175
     176    def _auto_id(self):
     177        auto_id = self._form.auto_id
     178        if auto_id and self._name:
     179            if '%s' in auto_id:
     180                auto_id = auto_id * self._name
     181            else:
     182                return self._name
     183        return ''
     184    auto_id = property(_auto_id)
     185 No newline at end of file
Back to Top