mirror of
https://github.com/django/django.git
synced 2025-07-25 14:14:13 +00:00
Made BoundFields iterable, so that you can iterate over individual radio buttons of a RadioSelect in a template
git-svn-id: http://code.djangoproject.com/svn/django/trunk@17173 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
0519adb2a8
commit
fc90c09efd
4 changed files with 87 additions and 1 deletions
|
@ -156,6 +156,15 @@ class Widget(object):
|
|||
memo[id(self)] = obj
|
||||
return obj
|
||||
|
||||
def subwidgets(self, name, value, attrs=None, choices=()):
|
||||
"""
|
||||
Yields all "subwidgets" of this widget. Used only by RadioSelect to
|
||||
allow template access to individual <input type="radio"> buttons.
|
||||
|
||||
Arguments are the same as for render().
|
||||
"""
|
||||
yield self
|
||||
|
||||
def render(self, name, value, attrs=None):
|
||||
"""
|
||||
Returns this Widget rendered as HTML, as a Unicode string.
|
||||
|
@ -628,6 +637,12 @@ class RadioInput(StrAndUnicode):
|
|||
self.index = index
|
||||
|
||||
def __unicode__(self):
|
||||
return self.render()
|
||||
|
||||
def render(self, name=None, value=None, attrs=None, choices=()):
|
||||
name = name or self.name
|
||||
value = value or self.value
|
||||
attrs = attrs or self.attrs
|
||||
if 'id' in self.attrs:
|
||||
label_for = ' for="%s_%s"' % (self.attrs['id'], self.index)
|
||||
else:
|
||||
|
@ -681,6 +696,10 @@ class RadioSelect(Select):
|
|||
self.renderer = renderer
|
||||
super(RadioSelect, self).__init__(*args, **kwargs)
|
||||
|
||||
def subwidgets(self, name, value, attrs=None, choices=()):
|
||||
for widget in self.get_renderer(name, value, attrs, choices):
|
||||
yield widget
|
||||
|
||||
def get_renderer(self, name, value, attrs=None, choices=()):
|
||||
"""Returns an instance of the renderer."""
|
||||
if value is None: value = ''
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue