我在一个中有2个表单,我添加了前缀以在视图中将其分开。但是我收到错误: __ init __()得到了一个意外的关键字参数'前缀'
我在堆栈上阅读有关前缀和其他问题的文档,但这对我没有帮助。
我的view.py:
@user_passes_test(lambda u: u.is_staff, login_url='/account/login/')
def client_create(request):
dict = {}
dict['title'] = 'New client'
if request.method == 'POST':
form_user = ClientUserCreateForm(request.POST, prefix="test1")
form_client = ClientCreateForm(request.POST, prefix="test2")
return HttpResponse(request.POST['first_name'])
# if form_user.is_valid() and form_client.is_valid():
# print(request.POST)
else:
dict['form_user'] = ClientUserCreateForm(prefix="test1")
dict['form_client'] = ClientCreateForm(prefix="test2")
return render(request, 'panel/client/create.html', dict)
我有两个表单,他们有init,当我从表单中删除它然后前缀工作正确。
我的forms.py
class ClientUserCreateForm(forms.ModelForm):
class Meta:
model = User
fields = ('first_name', 'last_name', 'email')
def __init__(self):
super(ClientUserCreateForm, self).__init__()
self.fields['first_name'].widget.attrs.update({
'type': 'text',
'class': 'form-control',
'id': 'input-text',
})
self.fields['last_name'].widget.attrs.update({
'type': 'text',
'class': 'form-control',
'id': 'input-text',
})
self.fields['email'].widget.attrs.update({
'type': 'text',
'class': 'form-control',
'id': 'input-text',
})
class ClientCreateForm(forms.ModelForm):
country = forms.CharField(max_length=2, widget=forms.Select(
choices=CountriesShortcut.objects.all().values_list('code', 'name').order_by('id')
))
forwarding_country = forms.CharField(max_length=2, widget=forms.Select(
choices=CountriesShortcut.objects.all().values_list('code', 'name').order_by('id')
))
class Meta:
model = Client
fields = ('name', 'vat', 'address', 'zip_code', 'city', 'country', 'forwarding_address',
'forwarding_zip_code', 'forwarding_city', 'forwarding_country', 'phone',
'mailing_email', 'regon')
def __init__(self):
super(ClientCreateForm, self).__init__()
self.fields['name'].widget.attrs.update({
'type': 'text',
'class': 'form-control',
'id': 'input-text',
})
self.fields['vat'].widget.attrs.update({
'type': 'text',
'class': 'form-control',
'id': 'input-text',
})
(....)
self.fields['regon'].widget.attrs.update({
'type': 'text',
'class': 'form-control',
'id': 'input-text',
})
回溯:
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/panel/client/create/
Django Version: 1.8.8
Python Version: 3.5.1
Installed Applications:
('django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'core',
'client',
'registration',
'avatar',
'filer',
'mptt',
'easy_thumbnails',
'reversion',
'guardian')
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 "C:\Users\loc\dJangoEnvironment\lib\site-packages\django\core\handlers\base.py" in get_response
132. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\loc\dJangoEnvironment\lib\site-packages\django\contrib\auth\decorators.py" in _wrapped_view
22. return view_func(request, *args, **kwargs)
File "C:\Users\loc\PycharmProjects\CRM\core\views.py" in client_create
72. dict['form_user'] = ClientUserCreateForm(prefix="test1")
Exception Type: TypeError at /panel/client/create/
Exception Value: __init__() got an unexpected keyword argument 'prefix'
回溯2 - 更新后__init __:
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/panel/client/create/
Django Version: 1.8.8
Python Version: 3.5.1
Installed Applications:
('django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'core',
'client',
'registration',
'avatar',
'filer',
'mptt',
'easy_thumbnails',
'reversion',
'guardian')
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 "C:\Users\loc\dJangoEnvironment\lib\site-packages\django\core\handlers\base.py" in get_response
132. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\loc\dJangoEnvironment\lib\site-packages\django\contrib\auth\decorators.py" in _wrapped_view
22. return view_func(request, *args, **kwargs)
File "C:\Users\loc\PycharmProjects\CRM\core\views.py" in client_create
73. dict['form_client'] = ClientCreateForm(prefix="test2")
File "C:\Users\loc\PycharmProjects\CRM\core\forms.py" in __init__
75. super(ClientUserCreateForm, self).__init__(*args, **kwargs)
Exception Type: TypeError at /panel/client/create/
Exception Value: super(type, obj): obj must be an instance or subtype of type
答案 0 :(得分:7)
您重写了表单的__init__
方法,并且您不接受新方法中的任何参数。它最容易接受并传递所有参数:
ClientUserCreateForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(ClientUserCreateForm, self).__init__(*args, **kwargs)