Login

boostrap append-input for widgets

Author:
DimmuR
Posted:
September 26, 2012
Language:
Python
Version:
1.3
Score:
0 (after 0 ratings)

Append 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=AppendWidget(base_widget=TextInput, data='@') )

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
class AppendWidget(Widget):
    u"""
    Widget that append 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(AppendWidget, 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-append">'
            u'    %(field)s<span class="add-on">%(data)s</span>'
            u'</div>'
        ) % {'field': field, 'data': self.data})

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 2 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 2 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 9 months, 2 weeks ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 10 months, 1 week ago
  5. Help text hyperlinks by sa2812 11 months ago

Comments

Please login first before commenting.