[英]Submit ChoiceField form Django
I'm dealing with how make a custom form in Django and now I'm stuck on the submit step. 我正在处理如何在Django中创建自定义表单,现在我陷入了提交步骤。
When I press submit, I'm getting an __init__() got multiple values for keyword argument 'networkList'
. 当我按Submit时,我得到一个__init__() got multiple values for keyword argument 'networkList'
。
My forms.py
it's: 我的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')
On my views.py
the method looks like: 在我的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})
The traceback error: 追溯错误:
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'
Looking at the doc, I read that the POST request it's always the first parameter ( request.Post
) and on the init method I think it corresponds with the self
, moreover on other answers here I saw that sometimes, putting data=params
works but in my case I get an error: need more than 1 value to unpack
on the line if form.is_valid():
查看文档,我发现POST请求始终是第一个参数( request.Post
),在init方法上,我认为它与self
相对应,此外,在其他答案上,我看到有时将data=params
可以使用,但是在我的情况下,我得到一个错误: if form.is_valid():
need more than 1 value to unpack
if form.is_valid():
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
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
So, where it's my mistake? 那么,这是我的错误所在? Thanks and regards. 谢谢并恭祝安康。
You've defined your form init so that the first positional argument is networkList
; 您已经定义了表单init,以便第一个位置参数是networkList
; so when you do form = SimpleDeploy(request.POST, networkList=None...)
, both the positional arg and the keyword arg both go to the same name, which isn't allowed. 因此,当您执行form = SimpleDeploy(request.POST, networkList=None...)
,位置arg和关键字arg都使用相同的名称,这是不允许的。
Don't change the signature at all; 根本不要更改签名; get the extra values from kwargs. 从kwargs获得额外的价值。
def __init__(self, *args, **kwargs):
networkList = kwargs.pop('networkList', None)
policiesList = kwargs.pop('policiesList', None)
applicationList = kwargs.pop('applicationList', None)
super(...)
Alternatively, since you only need those values inside the form, you might consider just passing the request to this method and getting those values directly there; 另外,由于您只需要表单中的那些值,因此您可以考虑将请求传递给此方法,然后直接在其中获取这些值。 the same syntax would apply. 相同的语法将适用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.