I'm dealing with how make a custom form in Django and now I'm stuck on the submit step.
When I press submit, I'm getting an __init__() got multiple values for keyword argument 'networkList'
.
My forms.py
it's:
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:
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():
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
; 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.
Don't change the signature at all; get the extra values from 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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.