本文整理汇总了Python中django.forms.fields.ChoiceField方法的典型用法代码示例。如果您正苦于以下问题:Python fields.ChoiceField方法的具体用法?Python fields.ChoiceField怎么用?Python fields.ChoiceField使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.forms.fields
的用法示例。
在下文中一共展示了fields.ChoiceField方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_context_data
# 需要导入模块: from django.forms import fields [as 别名]
# 或者: from django.forms.fields import ChoiceField [as 别名]
def get_context_data(self, **kwargs):
snippet_type = self.kwargs['service_type']
print(snippet_type)
context = super().get_context_data(**kwargs)
snippet_labels = dict()
snippet_labels['PAN-OS'] = 'panos'
snippet_labels['Panorama'] = 'panorama'
snippet_labels['Panorama-GPCS'] = 'panorama-gpcs'
snippet_labels['Templates'] = 'template'
snippet_labels['Terraform'] = 'terraform'
context['title'] = 'All Templates with type: %s' % snippet_type
context['header'] = 'Template Library'
services = snippet_utils.load_snippets_of_type(snippet_labels[snippet_type], self.app_dir)
form = context['form']
# we need to construct a new ChoiceField with the following basic format
# snippet_name = fields.ChoiceField(choices=(('gold', 'Gold'), ('silver', 'Silver'), ('bronze', 'Bronze')))
choices_list = list()
# grab each service and construct a simple tuple with name and label, append to the list
for service in services:
choice = (service['name'], service['label'])
choices_list.append(choice)
# let's sort the list by the label attribute (index 1 in the tuple)
choices_list = sorted(choices_list, key=lambda k: k[1])
# convert our list of tuples into a tuple itself
choices_set = tuple(choices_list)
# make our new field
new_choices_field = fields.ChoiceField(choices=choices_set, label='Choose Template:')
# set it on the original form, overwriting the hardcoded GSB version
form.fields['snippet_name'] = new_choices_field
context['form'] = form
return context
示例2: generate_dynamic_form
# 需要导入模块: from django.forms import fields [as 别名]
# 或者: from django.forms.fields import ChoiceField [as 别名]
def generate_dynamic_form(self, data=None) -> Form:
dynamic_form = super().generate_dynamic_form(data)
choices_list = [('offline', 'Offline'), ('online', 'Online')]
description = 'Validation Mode'
mode = self.get_value_from_workflow('mode', 'online')
default = mode
required = True
help_text = 'Online mode will pull configuration directly from an accessible PAN-OS device. Offline ' \
'allows an XML configuration file to be uploaded.'
dynamic_form.fields['mode'] = fields.ChoiceField(choices=choices_list,
label=description, initial=default,
required=required, help_text=help_text)
# Uncomment when skilletlib can take a config_source
# choices_list = list()
# candidate = ('candidate', 'Candidate')
# running = ('running', 'Running')
# choices_list.append(candidate)
# choices_list.append(running)
# dynamic_form.fields['config_source'] = fields.ChoiceField(widget=forms.Select, choices=tuple(choices_list),
# label='Configuration Source',
# initial='running', required=True,
# help_text='Which configuration file to use '
# 'for validation')
#
# f = dynamic_form.fields['config_source']
# w = f.widget
# w.attrs.update({'data-source': 'mode'})
# w.attrs.update({'data-value': 'online'})
#
return dynamic_form
示例3: dropdown
# 需要导入模块: from django.forms import fields [as 别名]
# 或者: from django.forms.fields import ChoiceField [as 别名]
def dropdown(cls, choice):
attrs = {
"class": "extra-widget extra-widget-dropdown",
"style": "display: none;",
}
return ChoiceField(
required=False,
choices=options_as_choices(choice),
widget=Select(attrs=attrs),
)