Login

boostrap prepend-input for widgets

Author:
DimmuR
Posted:
September 26, 2012
Language:
Python
Version:
1.3
Tags:
django bootstrap prepend-input
Score:
0 (after 0 ratings)

Prepend span with text, image or other data to any django widget so bootstrap can format it like in here (scroll to "Extending form controls" section)

Example usage:

example_field = CharField( max_length=255, min_length=1, label='Label', required=False, widget=PrependWidget(base_widget=TextInput, data='@') )

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
class PrependWidget(Widget):
    u"""
    Widget that prepend boostrap-style span with data to specified base widget
    """

    def __init__(self, base_widget, data, *args, **kwargs):
        u"""Initialise widget and get base instance"""
        super(PrependWidget, self).__init__(*args, **kwargs)
        self.base_widget = base_widget(*args, **kwargs)
        self.data = data

    def render(self, name, value, attrs=None):
        u"""Render base widget and add bootstrap spans"""
        field = self.base_widget.render(name, value, attrs)
        return mark_safe((
            u'<div class="input-prepend">'
            u'    <span class="add-on">%(data)s</span>%(field)s'
            u'</div>'
        ) % {'field': field, 'data': self.data})

More like this

  1. boostrap append-input for widgets by DimmuR 2 years, 10 months ago
  2. Radio widget with labels after inputs by avsd 3 years ago
  3. Bootstrap button dropdown widget (replaces forms.Select) by benjaoming 2 years, 12 months ago
  4. labeled hidden input with choices by eliasfernandez 2 years, 8 months ago
  5. Dynamically change a form select widget to a hidden widget by epicserve 6 years ago

Comments

Please login first before commenting.