Radio widget with labels after inputs

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
class ButtonRadioSelect(forms.RadioSelect):
    """Radio Select with overridden renderer, placing labels after inputs.

    To make 3d stateful buttons, add CSS::

        <style type="text/css">
          ul.form-button-radio li {display: inline-block;}
          ul.form-button-radio input[type="radio"] {display: none}
          ul.form-button-radio input[type="radio"]+label {
            padding: 2px;
            border-radius: 5px;
            -moz-border-radius: 5px;
            -webkit-border-radius: 5px;
            border: 2px outset #BBB;
            cursor: pointer;
          }
          ul.form-button-radio input[type="radio"]:checked+label {
            font-weight: bold;
            background-color: #999;
            color: white;
          }
        </style>

    """

    class ButtonRadioInput(forms.widgets.RadioInput):

        def __unicode__(self):
            # No idea, why Superclass' __unicode__ does not call
            # correct render() method
            return self.render()

        def render(self, name=None, value=None, attrs=None, choices=()):
            name = name or self.name
            value = value or self.value
            attrs = attrs or self.attrs
            if 'id' in self.attrs:
                label_for = ' for="%s_%s"' % (self.attrs['id'], self.index)
            else:
                label_for = ''
            choice_label = conditional_escape(force_unicode(self.choice_label))
            return mark_safe(u'%s <label%s>%s</label>' % (self.tag(), label_for, choice_label))

    class ButtonRadioFieldRenderer(forms.widgets.RadioFieldRenderer):
        def __iter__(self):
            for i, choice in enumerate(self.choices):
                yield ButtonRadioSelect.ButtonRadioInput(self.name, self.value,
                                       self.attrs.copy(), choice, i)

        def __getitem__(self, idx):
            choice = self.choices[idx] # Let the IndexError propogate
            return ButtonRadioSelect.ButtonRadioInput(self.name, self.value,
                                    self.attrs.copy(), choice, idx)

        def render(self):
            """Outputs a <ul> for this set of radio fields."""
            return mark_safe(u'<ul class="form-button-radio">\n%s\n</ul>' % u'\n'.join([u'<li>%s</li>'
                    % force_unicode(w) for w in self]))

    renderer = ButtonRadioFieldRenderer

More like this

  1. Multiple-Submit-Button Widget for Choice Field by Archatas 5 years, 8 months ago
  2. ChoiceField with widget in choice by alfonsopalomares 5 years, 1 month ago
  3. RadioSelectWithHelpText by moxypark 3 years, 8 months ago
  4. Simple Admin-button linking to Databrowse by magicrebirth 4 years, 12 months ago
  5. Display values from a bound (submitted) form by masida 1 month, 1 week ago

Comments

(Forgotten your password?)