提交 ChoiceField 表单 Django

Submit ChoiceField form Django

我正在处理如何在 Django 中制作自定义表单,现在我卡在了提交步骤。

当我按下提交时,我得到一个 __init__() got multiple values for keyword argument 'networkList'

我的forms.py是:

class SimpleDeploy(forms.Form):
    def __init__(self, networkList, policiesList, applicationList, *args, **kwargs):
        super(SimpleDeploy, self).__init__(*args, **kwargs)
        if networkList and policiesList and applicationList:
            self.fields['Network Partition ID:'] = forms.ChoiceField(choices=networkList)
            self.fields['Application Policy ID:'] = forms.ChoiceField(choices=policiesList)
            self.fields['Application ID:'] = forms.ChoiceField(choices=applicationList)
        else:
            self.fields['Network Partition ID:'] = forms.ChoiceField(choices='No network partitions found')
            self.fields['Application Policy ID'] = forms.ChoiceField(choices='No application policies found')
            self.fields['Application ID:'] = forms.ChoiceField(choices='No applications found')

在我的 views.py 上,方法如下所示:

def simpleDeploy(request):
    netList = getDetailsNetworkPartitions(request)
    polList = getDetailsApplicationPolicies(request)
    appList = getDetailsApplications(request)
    if request.method == 'POST':
        form = SimpleDeploy(request.POST, networkList=None, policiesList=None, applicationList=None)
        if form.is_valid():
            network = form.cleaned_data['Network Partition ID:']
            policy = form.cleaned_data['Application Policy ID:']
            application = form.cleaned_data['Application ID:']
            ##### more things
        else:
            simpleForm = SimpleDeploy(networkList=netList, policiesList=polList, applicationList=appList)
    else:
        simpleForm = SimpleDeploy(networkList=netList, policiesList=polList, applicationList=appList)
    return render(request, 'apacheStratos/simpleDeploy.html', {'form': simpleForm})

回溯错误:

Traceback:
File "/usr/local/lib/python2.7/dist-packages/Django-1.8.12-py2.7.egg/django/core/handlers/base.py" in get_response
  132.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/iago/Escritorio/tfm/website/apacheStratos/views.py" in simpleDeploy
  60.         form = SimpleDeploy(request.POST, networkList=None, policiesList=None, applicationList=None)

Exception Type: TypeError at /stratos/simpleDeploy
Exception Value: __init__() got multiple values for keyword argument 'networkList'

查看文档,我读到 POST 请求它始终是第一个参数 (request.Post) 并且在 init 方法上我认为它与 self 相对应,而且在这里的其他答案中,我看到有时,把 data=params 工作但在我的情况下我得到一个错误: need more than 1 value to unpack 在行 if form.is_valid():

回溯:

File "/usr/local/lib/python2.7/dist-packages/Django-1.8.12-py2.7.egg/django/core/handlers/base.py" in get_response
  132.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/iago/Escritorio/tfm/website/apacheStratos/views.py" in simpleDeploy
  61.         if form.is_valid():
File "/usr/local/lib/python2.7/dist-packages/Django-1.8.12-py2.7.egg/django/forms/forms.py" in is_valid
  184.         return self.is_bound and not self.errors
File "/usr/local/lib/python2.7/dist-packages/Django-1.8.12-py2.7.egg/django/forms/forms.py" in errors
  176.             self.full_clean()
File "/usr/local/lib/python2.7/dist-packages/Django-1.8.12-py2.7.egg/django/forms/forms.py" in full_clean
  392.         self._clean_fields()
File "/usr/local/lib/python2.7/dist-packages/Django-1.8.12-py2.7.egg/django/forms/forms.py" in _clean_fields
  407.                     value = field.clean(value)
File "/usr/local/lib/python2.7/dist-packages/Django-1.8.12-py2.7.egg/django/forms/fields.py" in clean
  163.         self.validate(value)
File "/usr/local/lib/python2.7/dist-packages/Django-1.8.12-py2.7.egg/django/forms/fields.py" in validate
  868.         if value and not self.valid_value(value):
File "/usr/local/lib/python2.7/dist-packages/Django-1.8.12-py2.7.egg/django/forms/fields.py" in valid_value
  878.         for k, v in self.choices:

Exception Type: ValueError at /stratos/simpleDeploy
Exception Value: need more than 1 value to unpack

所以,我的错误在哪里?谢谢和问候。

您已经定义了表单初始化,因此第一个位置参数是 networkList;因此,当您执行 form = SimpleDeploy(request.POST, networkList=None...) 时,位置参数和关键字参数都使用相同的名称,这是不允许的。

根本不要更改签名;从 kwargs 获取额外的值。

def __init__(self, *args, **kwargs):
    networkList = kwargs.pop('networkList', None)
    policiesList = kwargs.pop('policiesList', None)
    applicationList = kwargs.pop('applicationList', None)
    super(...)

或者,由于您只需要表单中的那些值,您可以考虑将请求传递给此方法并直接在那里获取这些值;将应用相同的语法。