本文整理匯總了Python中django.forms.widgets.CheckboxInput.check_test方法的典型用法代碼示例。如果您正苦於以下問題:Python CheckboxInput.check_test方法的具體用法?Python CheckboxInput.check_test怎麽用?Python CheckboxInput.check_test使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類django.forms.widgets.CheckboxInput
的用法示例。
在下文中一共展示了CheckboxInput.check_test方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: render
# 需要導入模塊: from django.forms.widgets import CheckboxInput [as 別名]
# 或者: from django.forms.widgets.CheckboxInput import check_test [as 別名]
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)
# No groups in widget so render checkboxes in normal way
if 'groups' not in final_attrs:
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 = format_html(' for="{0}"', final_attrs['id'])
else:
label_for = ''
cb = 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 = force_text(option_label)
output.append('<div class="checkbox"><label%s>%s %s</label></div>' % (label_for, rendered_cb, option_label))
return mark_safe('\n'.join(output))
output = []
groups = final_attrs['groups']
if 'groups' in final_attrs:
del final_attrs['groups']
for group_index, group in enumerate(groups):
str_values = set([force_text(v) for v in value])
group_id = 'group_%i' % group_index
result = ['<ul class="list-group">']
has_opened_checkboxes = False
for checkbox_index, (option_value, option_label) in enumerate(group[1]['choices']):
if has_id:
final_attrs = dict(final_attrs, id='%s_%s_%s' % (attrs['id'], group_index, checkbox_index))
label_for = format_html(' for="{0}"', final_attrs['id'])
else:
label_for = ''
cb = CheckboxInput(final_attrs, check_test=lambda value: value in str_values)
if cb.check_test(str(option_value)):
has_opened_checkboxes = True
break
open_group = False
for checkbox_index, (option_value, option_label) in enumerate(group[1]['choices']):
if has_id:
final_attrs = dict(final_attrs, id='%s_%s_%s' % (attrs['id'], group_index, checkbox_index))
label_for = format_html(' for="{0}"', final_attrs['id'])
else:
label_for = ''
if 'on_check' in final_attrs:
del final_attrs['on_check']
if has_opened_checkboxes is False and 'predefined_values_on_check' in group[1]:
predefined_values = group[1]['predefined_values_on_check']
if type(predefined_values) == str:
if predefined_values == 'all':
final_attrs['on_check'] = 'checked'
elif type(predefined_values) == list or type(predefined_values) == tuple:
if option_value in predefined_values:
final_attrs['on_check'] = 'checked'
cb = CheckboxInput(final_attrs, check_test=lambda value: value in str_values)
if cb.check_test(str(option_value)):
open_group = True
option_value = force_text(option_value)
rendered_cb = cb.render(name, option_value)
option_label = force_text(option_label)
result.append('<li class="list-group-item"><div class="checkbox"><label%s>%s %s</label></div></li>' % (label_for, rendered_cb, option_label))
result.append('</ul>')
output.append('<div class="group %(classes)s"><div class="panel panel-primary"><div class="panel-heading">%(heading)s</div>%(result)s</div></div>' % {
'heading': '<h4 class="panel-title checkbox"><label for="%(group_id)s">%(title)s</label></h4><a href="#" class="toggler"></a>' % {
'group_id': group_id,
'title': CheckboxInput().render(group_id, open_group, attrs={
'id': group_id,
}) + group[0],
},
'classes': ' '.join(group[1]['classes']) if 'classes' in group[1] else '',
'result': '\n'.join(result),
})
return mark_safe('\n'.join(output))