在 Django 的表单向导中使用来自 URL 的值
Using value from URL in Form Wizard for Django
我正在尝试使用 this Form Wizard 在 Django 中设计多页表单。
我需要从 URL 中获取一个值,这是一个客户的 ID,并将其传递给其中一个 Forms 实例,因为表单将使用该客户的特定值动态构建。
我已经尝试根据 重新定义方法 get_form_kwargs,但这对我不起作用。
我的 views.py 中有以下代码:
class NewScanWizard(CookieWizardView):
def done(self, form_list, **kwargs):
#some code
def get_form_kwargs(self, step):
kwargs = super(NewScanWizard, self).get_form_kwargs(step)
if step == '1': #I just need client_id for step 1
kwargs['client_id'] = self.kwargs['client_id']
return kwargs
那么,这是forms.py中的代码:
from django import forms
from clients.models import KnownHosts
from bson import ObjectId
class SetNameForm(forms.Form): #Form for step 0
name = forms.CharField()
class SetScopeForm(forms.Form): #Form for step 1, this is where I need to get client_id
def __init__(self, *args, **kwargs):
super(SetScopeForm, self).__init__(*args, **kwargs)
client_id = kwargs['client_id']
clientHosts = KnownHosts.objects.filter(_id=ObjectId(client_id))
if clientHosts:
for h in clientHosts.hosts:
#some code to build the form
当运行此代码时,第0步完美运行。但是,在提交第 0 部分并获取第 1 部分时,出现以下错误:
_init_() got an unexpected keyword argument 'client_id'
我进行了一些调试,我可以看到 client_id 的值正确绑定到 kwargs,但我不知道如何解决这个问题。我认为这可能不难解决,但我是 Python 的新手,不知道问题出在哪里。
您应该在调用 super(SetScopeForm, self).__init__(*args, **kwargs)
之前从 kwargs
中删除 cliend_id
。
要删除 client_id
,您可以使用 kwargs.pop('client_id', None)
:
class SetScopeForm(forms.Form): #Form for step 1, this is where I need to get client_id
def __init__(self, *args, **kwargs):
# POP CLIENT_ID BEFORE calling super SetScopeForm
client_id = kwargs.pop('client_id', None)
# call super
super(SetScopeForm, self).__init__(*args, **kwargs)
clientHosts = KnownHosts.objects.filter(_id=ObjectId(client_id))
if clientHosts:
for h in clientHosts.hosts:
#some code to build the form
我正在尝试使用 this Form Wizard 在 Django 中设计多页表单。 我需要从 URL 中获取一个值,这是一个客户的 ID,并将其传递给其中一个 Forms 实例,因为表单将使用该客户的特定值动态构建。
我已经尝试根据
class NewScanWizard(CookieWizardView):
def done(self, form_list, **kwargs):
#some code
def get_form_kwargs(self, step):
kwargs = super(NewScanWizard, self).get_form_kwargs(step)
if step == '1': #I just need client_id for step 1
kwargs['client_id'] = self.kwargs['client_id']
return kwargs
那么,这是forms.py中的代码:
from django import forms
from clients.models import KnownHosts
from bson import ObjectId
class SetNameForm(forms.Form): #Form for step 0
name = forms.CharField()
class SetScopeForm(forms.Form): #Form for step 1, this is where I need to get client_id
def __init__(self, *args, **kwargs):
super(SetScopeForm, self).__init__(*args, **kwargs)
client_id = kwargs['client_id']
clientHosts = KnownHosts.objects.filter(_id=ObjectId(client_id))
if clientHosts:
for h in clientHosts.hosts:
#some code to build the form
当运行此代码时,第0步完美运行。但是,在提交第 0 部分并获取第 1 部分时,出现以下错误:
_init_() got an unexpected keyword argument 'client_id'
我进行了一些调试,我可以看到 client_id 的值正确绑定到 kwargs,但我不知道如何解决这个问题。我认为这可能不难解决,但我是 Python 的新手,不知道问题出在哪里。
您应该在调用 super(SetScopeForm, self).__init__(*args, **kwargs)
之前从 kwargs
中删除 cliend_id
。
要删除 client_id
,您可以使用 kwargs.pop('client_id', None)
:
class SetScopeForm(forms.Form): #Form for step 1, this is where I need to get client_id
def __init__(self, *args, **kwargs):
# POP CLIENT_ID BEFORE calling super SetScopeForm
client_id = kwargs.pop('client_id', None)
# call super
super(SetScopeForm, self).__init__(*args, **kwargs)
clientHosts = KnownHosts.objects.filter(_id=ObjectId(client_id))
if clientHosts:
for h in clientHosts.hosts:
#some code to build the form