Python django.forms.MultiWidget() Examples

The following are 11 code examples for showing how to use django.forms.MultiWidget(). These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.

You may check out the related API usage on the sidebar.

You may also want to check out all available functions/classes of the module django.forms , or try the search function .

Example 1
Project: django-localized-fields   Author: SectorLabs   File: widgets.py    License: MIT License 5 votes vote down vote up
def get_context(self, name, value, attrs):
        context = super(forms.MultiWidget, self).get_context(name, value, attrs)
        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)

        final_attrs = context["widget"]["attrs"]
        input_type = final_attrs.pop("type", None)
        id_ = final_attrs.get("id")
        subwidgets = []
        for i, widget in enumerate(self.widgets):
            if input_type is not None:
                widget.input_type = input_type
            widget_name = "%s_%s" % (name, i)
            try:
                widget_value = value[i]
            except IndexError:
                widget_value = None
            if id_:
                widget_attrs = final_attrs.copy()
                widget_attrs["id"] = "%s_%s" % (id_, i)
            else:
                widget_attrs = final_attrs
            widget_attrs = self.build_widget_attrs(
                widget, widget_value, widget_attrs
            )
            widget_context = widget.get_context(
                widget_name, widget_value, widget_attrs
            )["widget"]
            widget_context.update(
                dict(lang_code=widget.lang_code, lang_name=widget.lang_name)
            )
            subwidgets.append(widget_context)
        context["widget"]["subwidgets"] = subwidgets
        return context 
Example 2
Project: djongo   Author: nesdis   File: test_clearablefileinput.py    License: GNU Affero General Public License v3.0 5 votes vote down vote up
def test_render_as_subwidget(self):
        """A ClearableFileInput as a subwidget of MultiWidget."""
        widget = MultiWidget(widgets=(self.widget,))
        self.check_html(widget, 'myfile', [FakeFieldFile()], html=(
            """
            Currently: <a href="something">something</a>
            <input type="checkbox" name="myfile_0-clear" id="myfile_0-clear_id">
            <label for="myfile_0-clear_id">Clear</label><br>
            Change: <input type="file" name="myfile_0">
            """
        )) 
Example 3
Project: djongo   Author: nesdis   File: test_radioselect.py    License: GNU Affero General Public License v3.0 5 votes vote down vote up
def test_render_as_subwidget(self):
        """A RadioSelect as a subwidget of MultiWidget."""
        choices = (('', '------'),) + self.beatles
        self.check_html(MultiWidget([self.widget(choices=choices)]), 'beatle', ['J'], html=(
            """<ul>
            <li><label><input type="radio" name="beatle_0" value=""> ------</label></li>
            <li><label><input checked type="radio" name="beatle_0" value="J"> John</label></li>
            <li><label><input type="radio" name="beatle_0" value="P"> Paul</label></li>
            <li><label><input type="radio" name="beatle_0" value="G"> George</label></li>
            <li><label><input type="radio" name="beatle_0" value="R"> Ringo</label></li>
            </ul>"""
        )) 
Example 4
Project: djongo   Author: nesdis   File: test_clearablefileinput.py    License: GNU Affero General Public License v3.0 5 votes vote down vote up
def test_render_as_subwidget(self):
        """A ClearableFileInput as a subwidget of MultiWidget."""
        widget = MultiWidget(widgets=(self.widget,))
        self.check_html(widget, 'myfile', [FakeFieldFile()], html=(
            """
            Currently: <a href="something">something</a>
            <input type="checkbox" name="myfile_0-clear" id="myfile_0-clear_id">
            <label for="myfile_0-clear_id">Clear</label><br>
            Change: <input type="file" name="myfile_0">
            """
        )) 
Example 5
Project: djongo   Author: nesdis   File: test_radioselect.py    License: GNU Affero General Public License v3.0 5 votes vote down vote up
def test_render_as_subwidget(self):
        """A RadioSelect as a subwidget of MultiWidget."""
        choices = (('', '------'),) + self.beatles
        self.check_html(MultiWidget([self.widget(choices=choices)]), 'beatle', ['J'], html=(
            """<ul>
            <li><label><input type="radio" name="beatle_0" value=""> ------</label></li>
            <li><label><input checked type="radio" name="beatle_0" value="J"> John</label></li>
            <li><label><input type="radio" name="beatle_0" value="P"> Paul</label></li>
            <li><label><input type="radio" name="beatle_0" value="G"> George</label></li>
            <li><label><input type="radio" name="beatle_0" value="R"> Ringo</label></li>
            </ul>"""
        )) 
Example 6
Project: ctf-gameserver   Author: fausecteam   File: form_as_div.py    License: ISC License 4 votes vote down vote up
def _get_css_classes(field):
    """
    Helper function which returns the appropriate (Bootstrap) CSS classes for rendering an input field and
    its `<div>` container.

    Returns:
        A tuple of the form `(div_classes, field_classes, iterate_subfields, wrap_in_label)`.
        `iterate_subfields` is a bool indicating that the field should be rendered by iterating over its
        sub-fields (i.e. over itself) and wrapping them in containers with `field_classes`. This is required
        for multiple radios and checkboxes belonging together.
        `wrap_in_label` is a bool that specifies whether the input field should be placed inside the
        associated `<label>`, as required by Bootstrap for checkboxes.
    """

    div_classes = []
    field_classes = []
    iterate_subfields = False
    wrap_in_label = False

    if field.errors:
        div_classes.append('has-error')

    widget = field.field.widget

    if isinstance(widget, CheckboxInput):
        div_classes.append('checkbox')
        wrap_in_label = True
    elif isinstance(widget, CheckboxSelectMultiple):
        div_classes.append('form-group')
        field_classes.append('checkbox')
        iterate_subfields = True
    elif isinstance(widget, RadioSelect):
        div_classes.append('form-group')
        field_classes.append('radio')
        iterate_subfields = True
    elif isinstance(widget, FileInput):
        pass
    elif isinstance(widget, MultiWidget):
        div_classes.append('form-group')
        div_classes.append('form-inline')
        field_classes.append('form-control')
    else:
        div_classes.append('form-group')
        field_classes.append('form-control')

    return (div_classes, field_classes, iterate_subfields, wrap_in_label) 
Example 7
Project: ctf-gameserver   Author: fausecteam   File: form_as_div.py    License: ISC License 4 votes vote down vote up
def _get_css_classes(field):
    """
    Helper function which returns the appropriate (Bootstrap) CSS classes for rendering an input field and
    its `<div>` container.

    Returns:
        A tuple of the form `(div_classes, field_classes, iterate_subfields, wrap_in_label)`.
        `iterate_subfields` is a bool indicating that the field should be rendered by iterating over its
        sub-fields (i.e. over itself) and wrapping them in containers with `field_classes`. This is required
        for multiple radios and checkboxes belonging together.
        `wrap_in_label` is a bool that specifies whether the input field should be placed inside the
        associated `<label>`, as required by Bootstrap for checkboxes.
    """

    div_classes = []
    field_classes = []
    iterate_subfields = False
    wrap_in_label = False

    if field.errors:
        div_classes.append('has-error')

    widget = field.field.widget

    if isinstance(widget, CheckboxInput):
        div_classes.append('checkbox')
        wrap_in_label = True
    elif isinstance(widget, CheckboxSelectMultiple):
        div_classes.append('form-group')
        field_classes.append('checkbox')
        iterate_subfields = True
    elif isinstance(widget, RadioSelect):
        div_classes.append('form-group')
        field_classes.append('radio')
        iterate_subfields = True
    elif isinstance(widget, FileInput):
        pass
    elif isinstance(widget, MultiWidget):
        div_classes.append('form-group')
        div_classes.append('form-inline')
        field_classes.append('form-control')
    else:
        div_classes.append('form-group')
        field_classes.append('form-control')

    return (div_classes, field_classes, iterate_subfields, wrap_in_label) 
Example 8
Project: ctf-gameserver   Author: fausecteam   File: form_as_div.py    License: ISC License 4 votes vote down vote up
def _get_css_classes(field):
    """
    Helper function which returns the appropriate (Bootstrap) CSS classes for rendering an input field and
    its `<div>` container.

    Returns:
        A tuple of the form `(div_classes, field_classes, iterate_subfields, wrap_in_label)`.
        `iterate_subfields` is a bool indicating that the field should be rendered by iterating over its
        sub-fields (i.e. over itself) and wrapping them in containers with `field_classes`. This is required
        for multiple radios and checkboxes belonging together.
        `wrap_in_label` is a bool that specifies whether the input field should be placed inside the
        associated `<label>`, as required by Bootstrap for checkboxes.
    """

    div_classes = []
    field_classes = []
    iterate_subfields = False
    wrap_in_label = False

    if field.errors:
        div_classes.append('has-error')

    widget = field.field.widget

    if isinstance(widget, CheckboxInput):
        div_classes.append('checkbox')
        wrap_in_label = True
    elif isinstance(widget, CheckboxSelectMultiple):
        div_classes.append('form-group')
        field_classes.append('checkbox')
        iterate_subfields = True
    elif isinstance(widget, RadioSelect):
        div_classes.append('form-group')
        field_classes.append('radio')
        iterate_subfields = True
    elif isinstance(widget, FileInput):
        pass
    elif isinstance(widget, MultiWidget):
        div_classes.append('form-group')
        div_classes.append('form-inline')
        field_classes.append('form-control')
    else:
        div_classes.append('form-group')
        field_classes.append('form-control')

    return (div_classes, field_classes, iterate_subfields, wrap_in_label) 
Example 9
Project: ctf-gameserver   Author: fausecteam   File: form_as_div.py    License: ISC License 4 votes vote down vote up
def _get_css_classes(field):
    """
    Helper function which returns the appropriate (Bootstrap) CSS classes for rendering an input field and
    its `<div>` container.

    Returns:
        A tuple of the form `(div_classes, field_classes, iterate_subfields, wrap_in_label)`.
        `iterate_subfields` is a bool indicating that the field should be rendered by iterating over its
        sub-fields (i.e. over itself) and wrapping them in containers with `field_classes`. This is required
        for multiple radios and checkboxes belonging together.
        `wrap_in_label` is a bool that specifies whether the input field should be placed inside the
        associated `<label>`, as required by Bootstrap for checkboxes.
    """

    div_classes = []
    field_classes = []
    iterate_subfields = False
    wrap_in_label = False

    if field.errors:
        div_classes.append('has-error')

    widget = field.field.widget

    if isinstance(widget, CheckboxInput):
        div_classes.append('checkbox')
        wrap_in_label = True
    elif isinstance(widget, CheckboxSelectMultiple):
        div_classes.append('form-group')
        field_classes.append('checkbox')
        iterate_subfields = True
    elif isinstance(widget, RadioSelect):
        div_classes.append('form-group')
        field_classes.append('radio')
        iterate_subfields = True
    elif isinstance(widget, FileInput):
        pass
    elif isinstance(widget, MultiWidget):
        div_classes.append('form-group')
        div_classes.append('form-inline')
        field_classes.append('form-control')
    else:
        div_classes.append('form-group')
        field_classes.append('form-control')

    return (div_classes, field_classes, iterate_subfields, wrap_in_label) 
Example 10
Project: ctf-gameserver   Author: fausecteam   File: form_as_div.py    License: ISC License 4 votes vote down vote up
def _get_css_classes(field):
    """
    Helper function which returns the appropriate (Bootstrap) CSS classes for rendering an input field and
    its `<div>` container.

    Returns:
        A tuple of the form `(div_classes, field_classes, iterate_subfields, wrap_in_label)`.
        `iterate_subfields` is a bool indicating that the field should be rendered by iterating over its
        sub-fields (i.e. over itself) and wrapping them in containers with `field_classes`. This is required
        for multiple radios and checkboxes belonging together.
        `wrap_in_label` is a bool that specifies whether the input field should be placed inside the
        associated `<label>`, as required by Bootstrap for checkboxes.
    """

    div_classes = []
    field_classes = []
    iterate_subfields = False
    wrap_in_label = False

    if field.errors:
        div_classes.append('has-error')

    widget = field.field.widget

    if isinstance(widget, CheckboxInput):
        div_classes.append('checkbox')
        wrap_in_label = True
    elif isinstance(widget, CheckboxSelectMultiple):
        div_classes.append('form-group')
        field_classes.append('checkbox')
        iterate_subfields = True
    elif isinstance(widget, RadioSelect):
        div_classes.append('form-group')
        field_classes.append('radio')
        iterate_subfields = True
    elif isinstance(widget, FileInput):
        pass
    elif isinstance(widget, MultiWidget):
        div_classes.append('form-group')
        div_classes.append('form-inline')
        field_classes.append('form-control')
    else:
        div_classes.append('form-group')
        field_classes.append('form-control')

    return (div_classes, field_classes, iterate_subfields, wrap_in_label) 
Example 11
Project: djongo   Author: nesdis   File: test_media.py    License: GNU Affero General Public License v3.0 4 votes vote down vote up
def test_multi_widget(self):
        ###############################################################
        # Multiwidget media handling
        ###############################################################

        class MyWidget1(TextInput):
            class Media:
                css = {
                    'all': ('path/to/css1', '/path/to/css2')
                }
                js = ('/path/to/js1', 'http://media.other.com/path/to/js2', 'https://secure.other.com/path/to/js3')

        class MyWidget2(TextInput):
            class Media:
                css = {
                    'all': ('/path/to/css2', '/path/to/css3')
                }
                js = ('/path/to/js1', '/path/to/js4')

        class MyWidget3(TextInput):
            class Media:
                css = {
                    'all': ('path/to/css1', '/path/to/css3')
                }
                js = ('/path/to/js1', '/path/to/js4')

        # MultiWidgets have a default media definition that gets all the
        # media from the component widgets
        class MyMultiWidget(MultiWidget):
            def __init__(self, attrs=None):
                widgets = [MyWidget1, MyWidget2, MyWidget3]
                super().__init__(widgets, attrs)

        mymulti = MyMultiWidget()
        self.assertEqual(
            str(mymulti.media),
            """<link href="http://media.example.com/static/path/to/css1" type="text/css" media="all" rel="stylesheet">
<link href="/path/to/css2" type="text/css" media="all" rel="stylesheet">
<link href="/path/to/css3" type="text/css" media="all" rel="stylesheet">
<script type="text/javascript" src="/path/to/js1"></script>
<script type="text/javascript" src="http://media.other.com/path/to/js2"></script>
<script type="text/javascript" src="https://secure.other.com/path/to/js3"></script>
<script type="text/javascript" src="/path/to/js4"></script>"""
        )