当前位置: 首页>>代码示例>>Python>>正文


Python fields.ChoiceField方法代码示例

本文整理汇总了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 
开发者ID:PaloAltoNetworks,项目名称:panhandler,代码行数:41,代码来源:views.py

示例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 
开发者ID:PaloAltoNetworks,项目名称:panhandler,代码行数:33,代码来源:views.py

示例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),
        ) 
开发者ID:project-callisto,项目名称:callisto-core,代码行数:12,代码来源:widgets.py


注:本文中的django.forms.fields.ChoiceField方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。