Login

boostrap prepend-input for widgets

Author:
DimmuR
Posted:
September 26, 2012
Language:
Python
Version:
1.3
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. Template tag - list punctuation for a list of items by shapiromatron 11 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months, 2 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 6 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 7 months ago
  5. Help text hyperlinks by sa2812 1 year, 7 months ago

Comments

Please login first before commenting.