Ich habe es zu tun, wie ein benutzerdefiniertes Formular in Django und jetzt bin ich auf den Schritt reichen.Submit ChoiceField Formular Django
Wenn ich auf "submit" drücke, erhalte ich eine __init__() got multiple values for keyword argument 'networkList'
.
Mein forms.py
es ist:
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')
Auf meinem views.py
das Verfahren wie folgt aussieht:
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})
Der Fehler Zurückverfolgungs:
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'
im doc Sehen, ich, dass das Lesen POST-Anfrage ist immer der erste Parameter (request.Post
) und auf der init-Methode finde ich es mit den self
entspricht im übrigen auf anderen Antworten hier sah ich, dass manchmal, Putting data=params
funktioniert, aber in meinem Fall bekomme ich einen Fehler: need more than 1 value to unpack
auf der Linie 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
Also, wo ist mein Fehler? Danke und Grüße.
Hallo Daniel, danke für deine Antwort. Ich habe die Signatur wie vorgeschlagen geändert und funktioniert jetzt. Aber ich bekomme immer noch "mehr als 1 Wert zum Entpacken in der Zeile, wenn form.is_valid():" Fehler. Ich kann auf den Post-Daten sehen, dass die Werte korrekt sind: (Variable \t Value) Netzwerk Partitions-ID: \t u'network-Partition-2' Anwendungs-ID: \t u'eb2ed6ea-922f-2e56-0129-c0e1c1899ec5' Application Policy ID: \t u'application-policy-2' csrfmiddlewaretoken u'BxsGuIE0iI80wJBkTEXF0sIEmLWXfn2t‘ Oder ich brauche auch ein Tupel übergeben? Vielen Dank. – Aker666
Sie sollten zeigen, was die 'getDetailsNetworkPartitions' und ähnliche Funktionen zurückgeben. Entscheidungen müssen eine Reihe von 2-Tupeln sein; gibst du nur eine einfache Liste zurück? –
Auf der submit, ja. Weil ich die Werte haben möchte, die der Benutzer gewählt hat, aber wenn ich das Formular die getDetailsNetworkPartitions (Anfrage), getDetailsApplicationPolicies (Anfrage), getDetailsApplications (Anfrage) fülle, geben sie ein Tupel wie (u'network-partition-2 ', u'network- Partition-2 '). Auf der submit, lesen auf anderen anwsers erhalten Sie das erste Element des Tupel (die ID der Wahl ausgewählt) oder habe ich falsch verstanden? Es muss auch ein Tupel sein? – Aker666