本文整理汇总了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
示例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">
"""
))
示例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>"""
))
示例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)
示例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>"""
)