當前位置: 首頁>>代碼示例>>Python>>正文


Python forms.MultiWidget方法代碼示例

本文整理匯總了Python中django.forms.MultiWidget方法的典型用法代碼示例。如果您正苦於以下問題:Python forms.MultiWidget方法的具體用法?Python forms.MultiWidget怎麽用?Python forms.MultiWidget使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在django.forms的用法示例。


在下文中一共展示了forms.MultiWidget方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: get_context

# 需要導入模塊: from django import forms [as 別名]
# 或者: from django.forms import MultiWidget [as 別名]
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 
開發者ID:SectorLabs,項目名稱:django-localized-fields,代碼行數:41,代碼來源:widgets.py

示例2: test_render_as_subwidget

# 需要導入模塊: from django import forms [as 別名]
# 或者: from django.forms import MultiWidget [as 別名]
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">
            """
        )) 
開發者ID:nesdis,項目名稱:djongo,代碼行數:13,代碼來源:test_clearablefileinput.py

示例3: test_render_as_subwidget

# 需要導入模塊: from django import forms [as 別名]
# 或者: from django.forms import MultiWidget [as 別名]
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>"""
        )) 
開發者ID:nesdis,項目名稱:djongo,代碼行數:14,代碼來源:test_radioselect.py

示例4: _get_css_classes

# 需要導入模塊: from django import forms [as 別名]
# 或者: from django.forms import MultiWidget [as 別名]
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) 
開發者ID:fausecteam,項目名稱:ctf-gameserver,代碼行數:48,代碼來源:form_as_div.py

示例5: test_multi_widget

# 需要導入模塊: from django import forms [as 別名]
# 或者: from django.forms import MultiWidget [as 別名]
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>"""
        ) 
開發者ID:nesdis,項目名稱:djongo,代碼行數:46,代碼來源:test_media.py


注:本文中的django.forms.MultiWidget方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。