我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用django.forms.CheckboxInput()。
def render(self, name, value, attrs=None): context = dict( value=value, input=super(forms.FileInput, self).render(name, value, attrs), for_label=attrs.get('id', ''), filename=os.path.basename(value.name) if value else '', ) if self.is_initial(value) and not self.is_required: checkbox_name = self.clear_checkbox_name(name) checkbox_id = self.clear_checkbox_id(checkbox_name) context['clear'] = forms.CheckboxInput().render( checkbox_name, False, attrs={ 'id': checkbox_id } ) return mark_safe(render_to_string('file_field/admin/widget.html', context))
def value_from_datadict(self, data, files, name): upload = super().value_from_datadict(data, files, name) if not self.is_required and forms.CheckboxInput().value_from_datadict( data, files, self.clear_checkbox_name(name)): if upload: # If the user contradicts themselves (uploads a new file AND # checks the "clear" checkbox), we return a unique marker # object that FileField will turn into a ValidationError. return FILE_INPUT_CONTRADICTION # False signals to clear any existing value, as opposed to just None return False return ( upload, data.get('%s-croparea' % name, None) or None )
def add_label(self, html): label = self.get_label() if hasattr(self.field.field, '_required'): required = self.field.field._required elif isinstance(self.field.field, I18nFormField): required = self.field.field.one_required else: required = self.field.field.required html = render_label( label, label_for=self.field.id_for_label, label_class=self.get_label_class(), optional=not required and not isinstance(self.widget, CheckboxInput) ) + html return html
def setUp(self): super(OmniModelFormTestCase, self).setUp() self.omniform = OmniModelFormFactory.create() self.field_1 = OmniCharField( name='title', label='Please give us a title', required=True, widget_class='django.forms.widgets.TextInput', order=0, initial='Some title...', form=self.omniform ) self.field_1.save() self.field_2 = OmniBooleanField( name='agree', label='Please agree', required=True, widget_class='django.forms.widgets.CheckboxInput', order=1, initial=True, form=self.omniform ) self.field_2.save() self.handler_1 = OmniFormEmailHandlerFactory.create(form=self.omniform) self.handler_2 = OmniFormEmailHandlerFactory.create(form=self.omniform)
def test_get_field(self): """ The _get_field method should return a form field instance """ field_1 = self.omniform._get_field('title') field_2 = self.omniform._get_field('agree') field_3 = self.omniform._get_field('fictional') self.assertIsInstance(field_1, forms.CharField) self.assertIsInstance(field_1.widget, forms.TextInput) self.assertEqual(field_1.label, 'Please give us a title') self.assertTrue(field_1.required) self.assertIsInstance(field_2, forms.BooleanField) self.assertIsInstance(field_2.widget, forms.CheckboxInput) self.assertEqual(field_2.label, 'Please agree') self.assertTrue(field_2.required) self.assertIsNone(field_3)
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
def is_checkbox(field): return isinstance(field.field.widget, forms.CheckboxInput)
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_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 class="checkbox-inline">%s %s</label>' % (label_for, rendered_cb, option_label)) else: output.append(u'<div class="checkbox"><label%s>%s %s</label></div>' % (label_for, rendered_cb, option_label)) return mark_safe(u'\n'.join(output))
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 class="checkbox-inline">%s %s</label>' % (label_for, rendered_cb, option_label)) else: output.append( u'<div class="checkbox"><label%s>%s %s</label></div>' % (label_for, rendered_cb, option_label)) return mark_safe(u'\n'.join(output))
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 class="checkbox-inline">%s %s</label>' % (label_for, rendered_cb, option_label)) else: output.append(u'<div class="checkbox"><label%s>%s %s</label></div>' % (label_for, rendered_cb, option_label)) return mark_safe(u'\n'.join(output))
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)
def is_checkbox(field): return field.field.widget.__class__.__name__ == CheckboxInput().__class__.__name__
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 = ' 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('<label%s class="checkbox-inline">%s %s</label>' % (label_for, rendered_cb, option_label)) else: output.append('<div class="checkbox"><label%s>%s %s</label></div>' % (label_for, rendered_cb, option_label)) return mark_safe('\n'.join(output))
def test_required_field_widget_optional(self): """ The form should use a checkbox widget for the required field if the corresponding model field is optional """ url = reverse('admin:omniforms_omnimodelform_createfield', args=[self.omni_form.pk, 'some_date']) response = self.client.get(url) self.assertIsInstance(response.context['form'].fields['required'].widget, forms.CheckboxInput)
def test_form_widgets(self): """ The model should define the correct form widgets """ self.assertIn('django.forms.widgets.CheckboxInput', OmniBooleanField.FORM_WIDGETS)