Last active
April 22, 2016 17:47
-
-
Save rmoch/6364899 to your computer and use it in GitHub Desktop.
- Django form widget for bootstrap switch HTML widget
- http://www.bootstrap-switch.org/
- https://github.com/nostalgiaz/bootstrap-switch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CheckBoxBootstrapSwitch(CheckboxInput): | |
"""Django widget for bootstrap switch HTML widget: http://www.bootstrap-switch.org/ | |
Options can be provided through 'switch' argument: | |
switch = forms.BooleanField(required=False, label=_(u"bootstrap switch"), | |
widget=CheckBoxBootstrapSwitch(switch={'size': 'small', 'on': 'warning', 'text-label': 'Switch Me'})) | |
""" | |
def __init__(self, switch=None, *args, **kwargs): | |
self.switch = switch or {} | |
super(CheckBoxBootstrapSwitch, self).__init__(*args, **kwargs) | |
def render(self, name, value, attrs=None): | |
checkbox = super(CheckBoxBootstrapSwitch, self).render(name=name, value=value, attrs=attrs) | |
data = '' | |
size = self.switch.get('size', '') # 'mini', 'small', 'large' or '' for normal | |
# handling of data-properties | |
for key in ['on', 'off', 'on-label', 'off-label', 'text-label']: | |
data += ' data-%s="%s"' % (key, self.switch[key]) if key in self.switch else '' | |
widget = '<div id="switch-%s" class="make-switch' % name | |
widget += ' switch-' + size if size else '' | |
widget += '"' + data +'>' + checkbox | |
widget += '</div>' | |
return mark_safe(widget) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment