當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。