mirror of
https://github.com/django/django.git
synced 2024-12-01 15:42:04 +01:00
Fixed #3025 -- Added auto_id option to Form.__init__(). Thanks, SmileyChris
git-svn-id: http://code.djangoproject.com/svn/django/trunk@4073 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
522f674070
commit
c4070e86c8
@ -23,8 +23,9 @@ class Form(object):
|
||||
"A collection of Fields, plus their associated data."
|
||||
__metaclass__ = DeclarativeFieldsMetaclass
|
||||
|
||||
def __init__(self, data=None): # TODO: prefix stuff
|
||||
def __init__(self, data=None, auto_id=False): # TODO: prefix stuff
|
||||
self.data = data or {}
|
||||
self.auto_id = auto_id
|
||||
self.clean_data = None # Stores the data after clean() has been called.
|
||||
self.__errors = None # Stores the errors after clean() has been called.
|
||||
|
||||
@ -156,6 +157,10 @@ class BoundField(object):
|
||||
errors = property(_errors)
|
||||
|
||||
def as_widget(self, widget, attrs=None):
|
||||
attrs = attrs or {}
|
||||
auto_id = self.auto_id
|
||||
if not attrs.has_key('id') and not widget.attrs.has_key('id') and auto_id:
|
||||
attrs['id'] = auto_id
|
||||
return widget.render(self._name, self._form.data.get(self._name, None), attrs=attrs)
|
||||
|
||||
def as_text(self, attrs=None):
|
||||
@ -167,3 +172,16 @@ class BoundField(object):
|
||||
def as_textarea(self, attrs=None):
|
||||
"Returns a string of HTML for representing this as a <textarea>."
|
||||
return self.as_widget(Textarea(), attrs)
|
||||
|
||||
def _auto_id(self):
|
||||
"""
|
||||
Calculates and returns the ID attribute for this BoundField, if the
|
||||
associated Form has specified auto_id. Returns an empty string otherwise.
|
||||
"""
|
||||
auto_id = self._form.auto_id
|
||||
if auto_id and '%s' in str(auto_id):
|
||||
return str(auto_id) % self._name
|
||||
elif auto_id:
|
||||
return self._name
|
||||
return ''
|
||||
auto_id = property(_auto_id)
|
||||
|
@ -973,6 +973,61 @@ u'* This field is required.'
|
||||
>>> print p['birthday']
|
||||
<input type="text" name="birthday" />
|
||||
|
||||
"auto_id" tells the Form to add an "id" attribute to each form element.
|
||||
If it's a string that contains '%s', Django will use that as a format string
|
||||
into which the field's name will be inserted.
|
||||
>>> p = Person(auto_id='id_%s')
|
||||
>>> print p.as_ul()
|
||||
<ul>
|
||||
<li>First name: <input type="text" name="first_name" id="id_first_name" /></li>
|
||||
<li>Last name: <input type="text" name="last_name" id="id_last_name" /></li>
|
||||
<li>Birthday: <input type="text" name="birthday" id="id_birthday" /></li>
|
||||
</ul>
|
||||
|
||||
If auto_id is any True value whose str() does not contain '%s', the "id"
|
||||
attribute will be the name of the field.
|
||||
>>> p = Person(auto_id=True)
|
||||
>>> print p.as_ul()
|
||||
<ul>
|
||||
<li>First name: <input type="text" name="first_name" id="first_name" /></li>
|
||||
<li>Last name: <input type="text" name="last_name" id="last_name" /></li>
|
||||
<li>Birthday: <input type="text" name="birthday" id="birthday" /></li>
|
||||
</ul>
|
||||
|
||||
If auto_id is any False value, an "id" attribute won't be output unless it
|
||||
was manually entered.
|
||||
>>> p = Person(auto_id=False)
|
||||
>>> print p.as_ul()
|
||||
<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" /></li>
|
||||
</ul>
|
||||
|
||||
In this example, auto_id is False, but the "id" attribute for the "first_name"
|
||||
field is given.
|
||||
>>> class PersonNew(Form):
|
||||
... first_name = CharField(widget=TextInput(attrs={'id': 'first_name_id'}))
|
||||
... last_name = CharField()
|
||||
... birthday = DateField()
|
||||
>>> p = PersonNew(auto_id=False)
|
||||
>>> print p.as_ul()
|
||||
<ul>
|
||||
<li>First name: <input type="text" id="first_name_id" name="first_name" /></li>
|
||||
<li>Last name: <input type="text" name="last_name" /></li>
|
||||
<li>Birthday: <input type="text" name="birthday" /></li>
|
||||
</ul>
|
||||
|
||||
If the "id" attribute is specified in the Form and auto_id is True, the "id"
|
||||
attribute in the Form gets precedence.
|
||||
>>> p = PersonNew(auto_id=True)
|
||||
>>> print p.as_ul()
|
||||
<ul>
|
||||
<li>First name: <input type="text" id="first_name_id" name="first_name" /></li>
|
||||
<li>Last name: <input type="text" name="last_name" id="last_name" /></li>
|
||||
<li>Birthday: <input type="text" name="birthday" id="birthday" /></li>
|
||||
</ul>
|
||||
|
||||
>>> class SignupForm(Form):
|
||||
... email = EmailField()
|
||||
... get_spam = BooleanField()
|
||||
|
Loading…
Reference in New Issue
Block a user