2016-07-14 4 views
0

Ich bin neu bei DJango und versuche, ein Choicefield-Formular mit benutzerdefinierten Daten zu füllen, aber ich erhalte einen Fehler, den ich nicht gut verstehe.Ich kann kein benutzerdefiniertes Choosefield-Formular auf Django ausfüllen.

Auf meinem views.py ich habe:

def simpleDeploy(request): 
    networkList = getDetailsNetworkPartitions(request) 
    policiesList = getDetailsApplicationPolicies(request) 
    if request.method == 'POST': 
     abs(5) #Don't do nothing by the moment, I need put something or I get an error 
    else: 
     simpleForm = SimpleDeploy(netList=networkList, polList=policiesList) 
    return render(request, 'apacheStratos/simpleDeploy.html', {'form': simpleForm}) 

Und mein forms.py ist:

class SimpleDeploy(forms.Form): 
    def __init__(self, networkList, policiesList, *args, **kwargs): 
     super(SimpleDeploy, self).__init__(*args, **kwargs) 
     if networkList and policiesList: 
      self.fields['networkPartitions'] = forms.ChoiceField(choices=networkList) 
      self.fields['applicationPolicies'] = forms.ChoiceField(choices=policiesList) 
     else: 
      self.fields['networkPartitions'] = forms.ChoiceField(choices='No network partitions found') 
      self.fields['applicationPolicies'] = forms.ChoiceField(choices='No application policies found') 

Der Fehler, der Django wirft es: __init__() takes at least 3 arguments (1 given) auf der Linie simpleForm = SimpleDeploy(netList=networkList, polList=policiesList)

Ich weiß nicht, Warum sagt das, das braucht 3 Argumente und gibt 1, wenn ich 2 passiere.

Was mache ich falsch? Vielen Dank.

EDITED ANZIEHT FEHLER UND TRACEBACK:

Fehler:

TypeError at /stratos/simpleDeploy 

__init__() takes at least 3 arguments (1 given) 

Request Method:  GET 
Request URL: http://127.0.0.1:8000/stratos/simpleDeploy 
Django Version:  1.8.12 
Exception Type:  TypeError 
Exception Value:  

__init__() takes at least 3 arguments (1 given) 

Exception Location:  /home/iago/Escritorio/tfm/website/apacheStratos/views.py in simpleDeploy, line 60 
Python Executable: /usr/bin/python2.7 
Python Version:  2.7.6 
Python Path:  

['/home/iago/Escritorio/tfm/website', 
'/usr/local/lib/python2.7/dist-packages/Django-1.8.12-py2.7.egg', 
'/home/iago/Escritorio/tfm/website', 
'/usr/lib/python2.7', 
'/usr/lib/python2.7/plat-x86_64-linux-gnu', 
'/usr/lib/python2.7/lib-tk', 
'/usr/lib/python2.7/lib-old', 
'/usr/lib/python2.7/lib-dynload', 
'/usr/local/lib/python2.7/dist-packages', 
'/usr/lib/python2.7/dist-packages', 
'/usr/lib/python2.7/dist-packages/PILcompat', 
'/usr/lib/python2.7/dist-packages/gtk-2.0', 
'/usr/lib/python2.7/dist-packages/ubuntu-sso-client'] 

Server time: Thu, 14 Jul 2016 11:39:42 +0000 

Traceback:

Environment: 


Request Method: GET 
Request URL: http://127.0.0.1:8000/stratos/simpleDeploy 

Django Version: 1.8.12 
Python Version: 2.7.6 
Installed Applications: 
('django.contrib.admin', 
'django.contrib.auth', 
'django.contrib.contenttypes', 
'django.contrib.sessions', 
'django.contrib.messages', 
'django.contrib.staticfiles', 
'contact', 
'menu', 
'catalog', 
'apacheStratos') 
Installed Middleware: 
('django.contrib.sessions.middleware.SessionMiddleware', 
'django.middleware.common.CommonMiddleware', 
'django.middleware.csrf.CsrfViewMiddleware', 
'django.contrib.auth.middleware.AuthenticationMiddleware', 
'django.contrib.auth.middleware.SessionAuthenticationMiddleware', 
'django.contrib.messages.middleware.MessageMiddleware', 
'django.middleware.clickjacking.XFrameOptionsMiddleware', 
'django.middleware.security.SecurityMiddleware') 


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.   simpleForm = SimpleDeploy(netList=networkList, polList=policiesList) 

Exception Type: TypeError at /stratos/simpleDeploy 
Exception Value: __init__() takes at least 3 arguments (1 given) 
+0

Bitte zeigen Sie die vollständige Traceback. –

+0

Hallo Daniel, ich habe meine Frage mit den Informationen aktualisiert. – Aker666

Antwort

2

Es sieht aus wie Sie einen anderen Schlüssel verwendet haben, und die Positions Argumente fehlen. Die Argumente, die Sie senden, werden als kwargs betrachtet.

In der __init__ des Formulars Sie erwarten folgende Argumente

def __init__(self, networkList, policiesList, *args, **kwargs): 

Aber Sie es mit den Tasten netList senden, und polList Daraus ergibt sich die Fehler.

Versuchen:

simpleForm = SimpleDeploy(networkList=netList, policiesList=polList) 

oder auch nur

simpleForm = SimpleDeploy(netList, polList) 

Bitte beachte, dass ich weiß nicht, was Ihre lokalen Variablen-Namen sind, so sie entsprechend aktualisieren.

+0

Oh, scheint zu arbeiten !!! Vielen Dank. Aber ich bekomme eine: zu viele Werte, um den Fehler in {form.as_table}} beim Template-Rendering zu entpacken. Warum? Wenn es ein Choicefiled ist, sollte es eine Liste von Strings mit den Auswahlmöglichkeiten akzeptieren. Oder ich liege falsch. – Aker666

+0

Ich denke, das Problem ist, es erwartet eine Liste von Tupel .. Sie möchten vielleicht in das Auswahlfeld Format django Formen zu sehen erwarten, dass es in – karthikr

+0

Ah, ok. Ich werde sehen. Vielen Dank! – Aker666

Verwandte Themen