Skip to content

Instantly share code, notes, and snippets.

@gjedeer
Created December 30, 2011 19:57
Show Gist options
  • Save gjedeer/1541232 to your computer and use it in GitHub Desktop.
Save gjedeer/1541232 to your computer and use it in GitHub Desktop.
from django.forms import widgets
from itertools import chain
from django.utils.encoding import StrAndUnicode, force_unicode
from django.utils.html import escape, conditional_escape
from django.utils.safestring import mark_safe
class CheckboxSelectMultiple(widgets.SelectMultiple):
def render(self, name, value, attrs=None, choices=()):
if value is None: value = []
has_id = attrs and 'id' in attrs
global_label = attrs['label'] if attrs and 'label' in attrs else None
final_attrs = self.build_attrs(attrs, name=name)
output = []
output.append(u'<ul class="inputs-list">')
# Normalize to strings
str_values = set([force_unicode(v) for v in value])
for i, (option_value, option_label) in enumerate(chain(self.choices, choices)):
# If an ID attribute was given, add a numeric index as a suffix,
# so that the checkboxes don't all have the same ID attribute.
if has_id:
final_attrs = dict(final_attrs, id='%s_%s' % (attrs['id'], i))
label_for = u' for="%s"' % final_attrs['id']
else:
label_for = ''
cb = widgets.CheckboxInput(final_attrs, check_test=lambda value: value in str_values)
option_value = force_unicode(option_value)
rendered_cb = cb.render(name, option_value)
option_label = conditional_escape(force_unicode(option_label))
output.append(u'<li><label%s>%s <span>%s</span></label></li>' % (label_for, rendered_cb, option_label))
output.append(u'</ul>')
return mark_safe(u'\n'.join(output))
def id_for_label(self, id_):
# See the comment for RadioSelect.id_for_label()
if id_:
id_ += '_0'
return id_
id_for_label = classmethod(id_for_label)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment