Here are the examples of the python api django.forms.CheckboxInput taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.
14 Examples
3
View Complete Implementation : forms.py
Copyright GNU Affero General Public License v3.0
Author : BirkbeckCTP
Copyright GNU Affero General Public License v3.0
Author : BirkbeckCTP
def __init__(self, *args, **kwargs):
super(SettingsForm, self).__init__(*args, **kwargs)
if 'instance' in kwargs:
press = kwargs['instance']
settings = press_models.PressSetting.objects.filter(press=press)
for setting in settings:
if setting.is_boolean:
self.fields[setting.name] = forms.BooleanField(widget=forms.CheckboxInput(), required=False)
else:
self.fields[setting.name] = forms.CharField(widget=forms.TextInput(), required=False)
self.fields[setting.name].initial = setting.value
3
View Complete Implementation : scripts.py
Copyright Apache License 2.0
Author : netbox-community
Copyright Apache License 2.0
Author : netbox-community
def as_field(self):
"""
Render the variable as a Django form field.
"""
form_field = self.form_field(**self.field_attrs)
if not isinstance(form_field.widget, forms.CheckboxInput):
form_field.widget.attrs['clast'] = 'form-control'
return form_field
3
View Complete Implementation : forms.py
Copyright Apache License 2.0
Author : netbox-community
Copyright Apache License 2.0
Author : netbox-community
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
exempt_widgets = [
forms.CheckboxInput, forms.ClearableFileInput, forms.FileInput, forms.RadioSelect
]
for field_name, field in self.fields.items():
if field.widget.__clast__ not in exempt_widgets:
css = field.widget.attrs.get('clast', '')
field.widget.attrs['clast'] = ' '.join([css, 'form-control']).strip()
if field.required and not isinstance(field.widget, forms.FileInput):
field.widget.attrs['required'] = 'required'
if 'placeholder' not in field.widget.attrs:
field.widget.attrs['placeholder'] = field.label
3
View Complete Implementation : test_checkboxinput.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
def test_render_check_test(self):
"""
You can past 'check_test' to the constructor. This is a callable that
takes the value and returns True if the box should be checked.
"""
widget = CheckboxInput(check_test=lambda value: value.startswith('hello'))
self.check_html(widget, 'greeting', '', html=(
'<input type="checkbox" name="greeting">'
))
self.check_html(widget, 'greeting', 'hello', html=(
'<input checked type="checkbox" name="greeting" value="hello">'
))
self.check_html(widget, 'greeting', 'hello there', html=(
'<input checked type="checkbox" name="greeting" value="hello there">'
))
self.check_html(widget, 'greeting', 'hello & goodbye', html=(
'<input checked type="checkbox" name="greeting" value="hello & goodbye">'
))
3
View Complete Implementation : test_checkboxinput.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
Copyright GNU Affero General Public License v3.0
Author : nesdis
def test_render_check_exception(self):
"""
Calling check_test() shouldn't swallow exceptions (#17888).
"""
widget = CheckboxInput(
check_test=lambda value: value.startswith('hello'),
)
with self.astertRaises(AttributeError):
widget.render('greeting', True)
3
View Complete Implementation : forms.py
Copyright Apache License 2.0
Author : respawner
Copyright Apache License 2.0
Author : respawner
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
custom_widgets = [forms.CheckboxInput, forms.RadioSelect]
for field_name, field in self.fields.items():
if field.widget.__clast__ in custom_widgets:
css = field.widget.attrs.get("clast", "")
field.widget.attrs["clast"] = " ".join(
[css, "custom-control-input"]
).strip()
else:
css = field.widget.attrs.get("clast", "")
field.widget.attrs["clast"] = " ".join([css, "form-control"]).strip()
if field.required:
field.widget.attrs["required"] = "required"
if "placeholder" not in field.widget.attrs:
field.widget.attrs["placeholder"] = field.label
0
View Complete Implementation : widgets.py
Copyright Apache License 2.0
Author : BeanWei
Copyright Apache License 2.0
Author : BeanWei
def render(self, name, value, attrs=None, choices=()):
if value is None:
value = []
has_id = attrs and 'id' in attrs
if DJANGO_11:
final_attrs = self.build_attrs(attrs, extra_attrs={'name': name})
else:
final_attrs = self.build_attrs(attrs, name=name)
output = []
# Normalize to strings
str_values = set([force_text(v) for v in value])
for i, (option_value, option_label) in enumerate(chain(self.choices, choices)):
# If an ID attribute was given, add a numeric index as a suffix,
# so that the checkboxes don't all have the same ID attribute.
if has_id:
final_attrs = dict(final_attrs, id='%s_%s' % (attrs['id'], i))
label_for = u' for="%s"' % final_attrs['id']
else:
label_for = ''
cb = forms.CheckboxInput(
final_attrs, check_test=lambda value: value in str_values)
option_value = force_text(option_value)
rendered_cb = cb.render(name, option_value)
option_label = conditional_escape(force_text(option_label))
if final_attrs.get('inline', False):
output.append(u'<label%s clast="checkbox-inline">%s %s</label>' % (label_for, rendered_cb, option_label))
else:
output.append(u'<div clast="checkbox"><label%s>%s %s</label></div>' % (label_for, rendered_cb, option_label))
return mark_safe(u'\n'.join(output))
0
View Complete Implementation : forms.py
Copyright GNU Affero General Public License v3.0
Author : BirkbeckCTP
Copyright GNU Affero General Public License v3.0
Author : BirkbeckCTP
def __init__(self, *args, **kwargs):
key_type = kwargs.pop('key_type', None)
value = kwargs.pop('value', None)
super(EditKey, self).__init__(*args, **kwargs)
if key_type == 'rich-text':
self.fields['value'].widget = SummernoteWidget()
elif key_type == 'boolean':
self.fields['value'].widget = forms.CheckboxInput()
elif key_type == 'integer':
self.fields['value'].widget = forms.TextInput(attrs={'type': 'number'})
elif key_type == 'file' or key_type == 'journalthumb':
self.fields['value'].widget = forms.FileInput()
elif key_type == 'text':
self.fields['value'].widget = forms.Textarea()
else:
self.fields['value'].widget.attrs['size'] = '100%'
self.fields['value'].initial = value
self.fields['value'].required = False
0
View Complete Implementation : helpers.py
Copyright MIT License
Author : bpgc-cte
Copyright MIT License
Author : bpgc-cte
def __init__(self, form, field, is_first):
self.field = form[field] # A django.forms.BoundField instance
self.is_first = is_first # Whether this field is first on the line
self.is_checkbox = isinstance(self.field.field.widget, forms.CheckboxInput)
self.is_readonly = False
0
View Complete Implementation : statusboard.py
Copyright GNU General Public License v2.0
Author : edigiacomo
Copyright GNU General Public License v2.0
Author : edigiacomo
@register.filter
def is_checkbox(field):
return isinstance(field.field.widget, forms.CheckboxInput)
0
View Complete Implementation : helpers.py
Copyright Apache License 2.0
Author : edisonlz
Copyright Apache License 2.0
Author : edisonlz
def __init__(self, form, field, is_first):
self.field = form[field] # A django.forms.BoundField instance
self.is_first = is_first # Whether this field is first on the line
self.is_checkbox = isinstance(self.field.field.widget, forms.CheckboxInput)
0
View Complete Implementation : widgets.py
Copyright GNU General Public License v3.0
Author : Liweimin0512
Copyright GNU General Public License v3.0
Author : Liweimin0512
def render(self, name, value, attrs=None, choices=()):
if value is None:
value = []
has_id = attrs and 'id' in attrs
final_attrs = self.build_attrs(attrs, name=name)
output = []
# Normalize to strings
str_values = set([force_unicode(v) for v in value])
for i, (option_value, option_label) in enumerate(chain(self.choices, choices)):
# If an ID attribute was given, add a numeric index as a suffix,
# so that the checkboxes don't all have the same ID attribute.
if has_id:
final_attrs = dict(final_attrs, id='%s_%s' % (attrs['id'], i))
label_for = u' for="%s"' % final_attrs['id']
else:
label_for = ''
cb = forms.CheckboxInput(
final_attrs, check_test=lambda value: value in str_values)
option_value = force_unicode(option_value)
rendered_cb = cb.render(name, option_value)
option_label = conditional_escape(force_unicode(option_label))
if final_attrs.get('inline', False):
output.append(u'<label%s clast="checkbox-inline">%s %s</label>' % (label_for, rendered_cb, option_label))
else:
output.append(u'<div clast="checkbox"><label%s>%s %s</label></div>' % (label_for, rendered_cb, option_label))
return mark_safe(u'\n'.join(output))
0
View Complete Implementation : widgets.py
Copyright MIT License
Author : Superbsco
Copyright MIT License
Author : Superbsco
def render(self, name, value, attrs=None, choices=()):
if value is None:
value = []
has_id = attrs and 'id' in attrs
final_attrs = self.build_attrs(attrs, extra_attrs={'name': name})
output = []
# Normalize to strings
str_values = set([force_text(v) for v in value])
for i, (option_value, option_label) in enumerate(chain(self.choices, choices)):
# If an ID attribute was given, add a numeric index as a suffix,
# so that the checkboxes don't all have the same ID attribute.
if has_id:
final_attrs = dict(final_attrs, id='%s_%s' % (attrs['id'], i))
label_for = u' for="%s"' % final_attrs['id']
else:
label_for = ''
cb = forms.CheckboxInput(
final_attrs, check_test=lambda value: value in str_values)
option_value = force_text(option_value)
rendered_cb = cb.render(name, option_value)
option_label = conditional_escape(force_text(option_label))
if final_attrs.get('inline', False):
output.append(u'<label%s clast="checkbox-inline">%s %s</label>' % (label_for, rendered_cb, option_label))
else:
output.append(u'<div clast="checkbox"><label%s>%s %s</label></div>' % (label_for, rendered_cb, option_label))
return mark_safe(u'\n'.join(output))
0
View Complete Implementation : widgets.py
Copyright BSD 3-Clause "New" or "Revised" License
Author : vip68
Copyright BSD 3-Clause "New" or "Revised" License
Author : vip68
def render(self, name, value, attrs=None, choices=()):
if value is None:
value = []
has_id = attrs and 'id' in attrs
final_attrs = self.build_attrs(attrs, extra_attrs={'name': name})
output = []
# Normalize to strings
str_values = set([force_text(v) for v in value])
for i, (option_value, option_label) in enumerate(chain(self.choices, choices)):
# If an ID attribute was given, add a numeric index as a suffix,
# so that the checkboxes don't all have the same ID attribute.
if has_id:
final_attrs = dict(final_attrs, id='%s_%s' % (attrs['id'], i))
label_for = u' for="%s"' % final_attrs['id']
else:
label_for = ''
cb = forms.CheckboxInput(
final_attrs, check_test=lambda value: value in str_values)
option_value = force_text(option_value)
rendered_cb = cb.render(name, option_value)
option_label = conditional_escape(force_text(option_label))
final_attrs['inline'] = True if self.attrs['inline'] else False
if final_attrs.get('inline', False):
output.append(u'<label%s clast="checkbox-inline">%s %s</label>' % (label_for, rendered_cb, option_label))
else:
output.append(u'<div clast="checkbox"><label%s>%s %s</label></div>' % (label_for, rendered_cb, option_label))
return mark_safe(u'\n'.join(output))