Commits

trustrachel  committed f1ff6df

Enabling manual selection of objects via templates

  • Participants
  • Parent commits 7a4cebf

Comments (0)

Files changed (1)

File wtforms/widgets/core.py

         if k in ('class_', 'class__', 'for_'):
             k = k[:-1]
         if v is True:
-            params.append(k)
+          params.append(u'%s="%s"' % (unicode(k), escape(unicode(k), quote=True)))
         else:
-            params.append(u'%s="%s"' % (unicode(k), escape(unicode(v), quote=True)))
+          params.append(u'%s="%s"' % (unicode(k), escape(unicode(v), quote=True)))
+
     return u' '.join(params)
 
 
         kwargs.setdefault('id', field.id)
         if self.multiple:
             kwargs['multiple'] = True
+
+        options_kwargs = dict()
+        if 'selected' in kwargs:
+          # we're already passing "selected" as an option, so we have to choose
+          # another name temporarily
+          option_kwargs['manually_selected'] = unicode(kwargs['selected'])
+          del kwargs['selected']
+
         html = [u'<select %s>' % html_params(name=field.name, **kwargs)]
         for val, label, selected in field.iter_choices():
-            html.append(self.render_option(val, label, selected))
+            html.append(self.render_option(val, label, selected, **option_kwargs))
         html.append(u'</select>')
         return HTMLString(u''.join(html))
 
     @classmethod
     def render_option(cls, value, label, selected, **kwargs):
         options = dict(kwargs, value=value)
-        if selected:
+        if 'manually_selected' in kwargs:
+          if unicode(value) == unicode(kwargs['manually_selected']):
             options['selected'] = True
+          del options['manually_selected']
+        elif selected:
+          options['selected'] = True
+
+
         return HTMLString(u'<option %s>%s</option>' % (html_params(**options), escape(unicode(label))))