无法在Django上填写自定义选择字段表单

时间:2016-07-14 11:37:24

标签: python django forms choicefield

我是DJango的新手,我试图用自定义数据填写一个选择字段表格,但我收到的错误是我不太了解。

views.py我有:

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})

我的forms.py是:

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')

Django在__init__() takes at least 3 arguments (1 given)行上引发错误simpleForm = SimpleDeploy(netList=networkList, polList=policiesList)的错误

我不知道为什么说有3个参数,如果我通过2则给出1。

我做错了什么?感谢。

编辑错误和追溯:

错误:

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

回溯:

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)

1 个答案:

答案 0 :(得分:2)

看起来您使用了不同的密钥,并且缺少位置参数。您发送的参数被视为kwargs

在表格__init__中,您期待以下参数

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

但是您使用密钥netListpolList发送它因此错误。

尝试:

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

甚至只是

simpleForm = SimpleDeploy(netList, polList)

请注意,我不知道您的本地变量名称是什么,因此请相应地更新它们。