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
- Template tag - list punctuation for a list of items by shapiromatron 10 months, 1 week ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 2 weeks ago
- Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
- Help text hyperlinks by sa2812 1 year, 6 months ago
Comments
Please login first before commenting.