Python django.forms.widgets 模块,CheckboxInput() 实例源码

我们从Python开源项目中,提取了以下12个代码示例,用于说明如何使用django.forms.widgets.CheckboxInput()

项目:jiango    作者:yefei    | 项目源码 | 文件源码
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 = widgets.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))
            output.append(u'<label class="%s"%s>%s %s</label>' % (self.label_class,
                                                                  label_for, rendered_cb, option_label))
        return mark_safe(u'\n'.join(output))
项目:maas    作者:maas    | 项目源码 | 文件源码
def test_DictCharWidget_renders_fieldset_with_label_and_field_names(self):
        names = [factory.make_string(), factory.make_string()]
        initials = []
        labels = [factory.make_string(), factory.make_string()]
        values = [factory.make_string(), factory.make_string()]
        widget = DictCharWidget(
            [widgets.TextInput, widgets.TextInput, widgets.CheckboxInput],
            names, initials, labels, skip_check=True)
        name = factory.make_string()
        html_widget = fromstring(
            '<root>' + widget.render(name, values) + '</root>')
        widget_names = XPath('fieldset/input/@name')(html_widget)
        widget_labels = XPath('fieldset/label/text()')(html_widget)
        widget_values = XPath('fieldset/input/@value')(html_widget)
        expected_names = [
            "%s_%s" % (name, widget_name) for widget_name in names]
        self.assertEqual(
            [expected_names, labels, values],
            [widget_names, widget_labels, widget_values])
项目:maas    作者:maas    | 项目源码 | 文件源码
def test_DictCharWidget_renders_with_empty_string_as_input_data(self):
        names = [factory.make_string(), factory.make_string()]
        initials = []
        labels = [factory.make_string(), factory.make_string()]
        widget = DictCharWidget(
            [widgets.TextInput, widgets.TextInput, widgets.CheckboxInput],
            names, initials, labels, skip_check=True)
        name = factory.make_string()
        html_widget = fromstring(
            '<root>' + widget.render(name, '') + '</root>')
        widget_names = XPath('fieldset/input/@name')(html_widget)
        widget_labels = XPath('fieldset/label/text()')(html_widget)
        expected_names = [
            "%s_%s" % (name, widget_name) for widget_name in names]
        self.assertEqual(
            [expected_names, labels],
            [widget_names, widget_labels])
项目:django-bootstrap4    作者:zostera    | 项目源码 | 文件源码
def is_widget_required_attribute(widget):
    """
    Is this widget required?
    """
    if not widget.is_required:
        return False
    if isinstance(widget, (AdminFileWidget, HiddenInput, FileInput, CheckboxInput, CheckboxSelectMultiple)):
        return False
    return True
项目:Markovstuck    作者:Matoking    | 项目源码 | 文件源码
def is_checkbox(field):
    return isinstance(field.field.widget, CheckboxInput)
项目:adhocracy4    作者:liqd    | 项目源码 | 文件源码
def value_from_datadict(self, data, files, name):
        """
        Modify value_from_datadict, so that delete takes precedence over
        upload.
        """
        file_value = super(widgets.ClearableFileInput, self)\
            .value_from_datadict(data, files, name)
        checkbox_value = widgets.CheckboxInput()\
            .value_from_datadict(data, files, self.clear_checkbox_name(name))
        if not self.is_required and checkbox_value:
            return False
        return file_value
项目:a4-opin    作者:liqd    | 项目源码 | 文件源码
def value_from_datadict(self, data, files, name):
        file_value = super(widgets.ClearableFileInput, self) \
            .value_from_datadict(data, files, name)
        checkbox_value = widgets.CheckboxInput() \
            .value_from_datadict(data, files, self.clear_checkbox_name(name))
        if not self.is_required and checkbox_value:
            return False
        return file_value
项目:maas    作者:maas    | 项目源码 | 文件源码
def test_empty_DictCharWidget_renders_as_empty_string(self):
        widget = DictCharWidget(
            [widgets.CheckboxInput], [], [], [], skip_check=True)
        self.assertEqual('', widget.render(factory.make_string(), ''))
项目:old-web    作者:cualbondi    | 项目源码 | 文件源码
def is_checkbox(value):
    if not isinstance(value, BoundField):
        return False
    return isinstance(value.field.widget, CheckboxInput)
项目:jenkins-panel    作者:nikitinsm    | 项目源码 | 文件源码
def render(self, name, value, attrs=None):
        if self.is_localized:
            for widget in self.widgets:
                widget.is_localized = self.is_localized
        # value is a list of values, each corresponding to a widget
        # in self.widgets.
        if not isinstance(value, list):
            value = self.decompress(value)
        output = []
        final_attrs = self.build_attrs(attrs)
        id_ = final_attrs.get('id', None)
        output.append('<table class="table table-striped table-bordered table-hover table-condensed" style="width: 50%; margin: 0">')
        for i, widget in enumerate(self.widgets):
            try:
                widget_value = value[i]
            except IndexError:
                widget_value = None

            field = widget.attrs.pop('field', None)

            final_attrs['style'] = 'width: auto'
            if type(widget) in (widgets.CheckboxInput, ):
                if widget_value == 'false':
                    widget_value = None

            if id_:
                final_attrs = dict(final_attrs, id='%s_%s' % (id_, i))

            output.append('<tr>')
            output.append('<td style="width: 30%">')
            if field:
                output.append('<label>%s</label>' % field.label)
            output.append('</td>')
            output.append('<td>')
            output.append(widget.render(name + '_%s' % i, widget_value, final_attrs))
            if field.help_text:
                output.append('<p class="help-block">')
                output.append(field.help_text)
                output.append('</p>')
            output.append('</td>')
            output.append('<tr>')
        output.append('</table>')
        return mark_safe(self.format_output(output))
项目:adhocracy4    作者:liqd    | 项目源码 | 文件源码
def render(self, name, value, attrs=None):
        html_id = attrs and attrs.get('id', name) or name
        has_image_set = self.is_initial(value)
        is_required = self.is_required

        file_placeholder = ugettext('Select a picture from your local folder.')
        file_input = super().render(name, None, {
            'id': html_id,
            'class': 'form-control form-control-file'
        })

        if has_image_set:
            file_name = basename(value.name)
            file_url = conditional_escape(value.url)
        else:
            file_name = ""
            file_url = ""

        text_input = widgets.TextInput().render('__noname__', file_name, {
            'class': 'form-control form-control-file-dummy',
            'placeholder': file_placeholder,
            'tabindex': '-1',
            'id': 'text-{}'.format(html_id)
        })

        checkbox_id = self.clear_checkbox_id(name)
        checkbox_name = self.clear_checkbox_name(name)
        checkbox_input = widgets.CheckboxInput().render(checkbox_name, False, {
            'id': checkbox_id,
            'class': 'clear-image',
            'data-upload-clear': html_id,
        })

        context = {
            'id': html_id,
            'has_image_set': has_image_set,
            'is_required': is_required,
            'file_url': file_url,
            'file_input': file_input,
            'file_id': html_id + '-file',
            'text_input': text_input,
            'checkbox_input': checkbox_input,
            'checkbox_id': checkbox_id
        }

        return loader.render_to_string(
            'a4images/image_upload_widget.html',
            context
        )
项目:a4-opin    作者:liqd    | 项目源码 | 文件源码
def render(self, name, value, attrs=None):
        has_file_set = self.is_initial(value)
        is_required = self.is_required

        file_placeholder = ugettext('Select a file from your local folder.')
        file_input = super().render(name, None, {
            'id': name,
            'class': 'form-control form-control-file'
        })

        if has_file_set:
            file_name = basename(value.name)
            file_url = conditional_escape(value.url)
        else:
            file_name = ""
            file_url = ""

        text_input = widgets.TextInput().render('__noname__', file_name, {
            'class': 'form-control form-control-file-dummy',
            'placeholder': file_placeholder
        })

        checkbox_id = self.clear_checkbox_id(name)
        checkbox_name = self.clear_checkbox_name(name)
        checkbox_input = widgets.CheckboxInput().render(checkbox_name, False, {
            'id': checkbox_id,
            'class': 'clear-image',
            'data-upload-clear': name,
        })

        context = {
            'name': name,
            'has_image_set': has_file_set,
            'is_required': is_required,
            'file_url': file_url,
            'file_input': file_input,
            'file_id': name + '-file',
            'text_input': text_input,
            'checkbox_input': checkbox_input,
            'checkbox_id': checkbox_id
        }

        return mark_safe(
            loader.render_to_string(
                'euth_offlinephases/file_upload_widget.html',
                context
            )
        )