class SelectWidgetBootstrap(forms.Select): """ http://twitter.github.com/bootstrap/components.html#buttonDropdowns Needs bootstrap and jquery """ js = (""" """) def __init__(self, attrs={'class': 'btn-group pull-left btn-group-form'}, choices=()): self.noscript_widget = forms.Select(attrs={}, choices=choices) super(SelectWidgetBootstrap, self).__init__(attrs, choices) def __setattr__(self, k, value): super(SelectWidgetBootstrap, self).__setattr__(k, value) if k != 'attrs': self.noscript_widget.__setattr__(k, value) def render(self, name, value, attrs=None, choices=()): if value is None: value = '' final_attrs = self.build_attrs(attrs, name=name) output = ["""""" """ """ """ """ """ """ """ """ """""" """%(js)s""" """""" % {'attrs': flatatt(final_attrs), 'options':self.render_options(choices, [value]), 'label': _(u'Select an option'), 'name': name, 'js': SelectWidgetBootstrap.js, 'noscript': self.noscript_widget.render(name, value, {}, choices)} ] return mark_safe(u'\n'.join(output)) def render_option(self, selected_choices, option_value, option_label): option_value = force_unicode(option_value) selected_html = (option_value in selected_choices) and u' selected="selected"' or '' return u'
  • %s
  • ' % ( escape(option_value), selected_html, conditional_escape(force_unicode(option_label))) def render_options(self, choices, selected_choices): # Normalize to strings. selected_choices = set([force_unicode(v) for v in selected_choices]) output = [] for option_value, option_label in chain(self.choices, choices): if isinstance(option_label, (list, tuple)): output.append(u'
  • ' % escape(force_unicode(option_value))) for option in option_label: output.append(self.render_option(selected_choices, *option)) else: output.append(self.render_option(selected_choices, option_value, option_label)) return u'\n'.join(output)