Ticket #4003: SelectWTitle.diff

File SelectWTitle.diff, 1.7 KB (added by tonnzor <tonn81@…>, 17 years ago)
  • fields.py

     
    357357        value = smart_unicode(value)
    358358        if value == u'':
    359359            return value
    360         valid_values = set([str(k) for k, v in self.choices])
     360        valid_values = set([str(choice[0]) for choice in self.choices])
    361361        if value not in valid_values:
    362362            raise ValidationError(gettext(u'Select a valid choice. That choice is not one of the available choices.'))
    363363        return value
  • widgets.py

     
    172172        output.append(u'</select>')
    173173        return u'\n'.join(output)
    174174
     175class SelectWTitle(Select):
     176    def render(self, name, value, attrs=None, choices=()):
     177        if value is None: value = ''
     178        final_attrs = self.build_attrs(attrs, name=name)
     179        output = [u'<select%s>' % flatatt(final_attrs)]
     180        str_value = smart_unicode(value) # Normalize to string.
     181        for option_value, option_label, option_title in chain(self.choices, choices):
     182            option_value = smart_unicode(option_value)
     183            selected_html = (option_value == str_value) and u' selected="selected"' or ''
     184            output.append(u'<option value="%s"%s title="%s">%s</option>' % (escape(option_value), selected_html, escape(smart_unicode(option_title)), escape(smart_unicode(option_label))))
     185        output.append(u'</select>')
     186        return u'\n'.join(output)
     187
    175188class NullBooleanSelect(Select):
    176189    """
    177190    A Select Widget intended to be used with NullBooleanField.
Back to Top