As for my second question, this is the solution. Expanding Class:
from django import forms from django.utils.encoding import force_unicode from itertools import chain from django.utils.html import escape, conditional_escape class Select(forms.Select): """ A subclass of Select that adds the possibility to define additional properties on options. It works as Select, except that the ``choices`` parameter takes a list of 3 elements tuples containing ``(value, label, attrs)``, where ``attrs`` is a dict containing the additional attributes of the option. """ def render_options(self, choices, selected_choices): def render_option(option_value, option_label, attrs): option_value = force_unicode(option_value) selected_html = (option_value in selected_choices) and u' selected="selected"' or '' attrs_html = [] for k, v in attrs.items(): attrs_html.append('%s="%s"' % (k, escape(v))) if attrs_html: attrs_html = " " + " ".join(attrs_html) else: attrs_html = "" return u'<option value="{0}"{1}{2}>{3}</option>'.format( escape(option_value), selected_html, attrs_html, conditional_escape(force_unicode(option_label)) ) ''' return u'<option value="%s"%s%s>%s</option>' % ( escape(option_value), selected_html, attrs_html, conditional_escape(force_unicode(option_label))) '''
Example:
OPTIONS = [ ["AUT", "Australia", {'selected':'selected', 'data-index':'1'}], ["DEU", "Germany", {'selected':'selected'}], ["NLD", "Neitherlands", {'selected':'selected'}], ["USA", "United States", {}] ]
CodeArtist
source share