django形式:将参数从view.py传递给表单发出错误 [英] django form: Passing parameter from view.py to forms gives out error
问题描述
新手问题:
我需要从一个form.py中的一个表单中接受一个参数,但是它给了我很多麻烦。在视图中,我创建了一个包含以下代码片段的方法:
def scan_page(request):
myClient = request.user .get_profile()。client
form = WirelessScanForm(client = myClient)#pass参数到
并在forms.py中定义了以下格式:
class WirelessScanForm(forms.ModelForm):
time = forms.DateTimeField(label =Schedule Time,widget = AdminSplitDateTime())
def __init __(self,* args,** kwargs):
myClient = kwargs.pop (client)#client是从views.py
super(WirelessScanForm,self)传递的参数。_ init __(* args,** kwargs)
prob = forms.ChoiceField(label =Sniffer ,choices = [x.sniffer.plug_ip for x in Sniffer.objects.filter(client = myClient)])
但是django不断给我错误说: TemplateSyntaxError:在渲染时捕获NameError:name'myClient'不是def in
(查询中发生此错误)
我恐怕这里会有一些愚蠢的错误,但是我无法真正弄清楚为什么。请帮助,谢谢。
假设我正确地更正了格式,你有一个缩进问题: prob
在 __ init __
之外,因此无法访问本地 myClient
变量但是,如果你把它带入方法,它仍然不会工作,因为还有另外两个问题:首先,简单地将一个字段赋给一个变量,赢得'将它放在表格上;其次,选项
属性需要一个2元组列表,而不仅仅是平面列表。您需要的是:
def __init __(self,* args,** kwargs):
pre>
myClient = kwargs .pop(client)#client是从views.py
super(WirelessScanForm,self)传递的参数.__ init __(* args,** kwargs)
self.fields ['prob'] =在Sniffer.objects.filter(client = myClient)中的x的forms.ChoiceField(label =Sniffer,choices = [(x.plug_ip,x.MY_DESCRIPTIVE_FIELD)])
显然将
MY_DESCRIPTIVE_FIELD
替换为要在选项中显示的实际字段。Newbie question: I need to accept a parameter in a form from a method in views.py but it gave me troubles. In the view I created a method with following snippet:
def scan_page(request): myClient = request.user.get_profile().client form = WirelessScanForm(client = myClient) # pass parameter to the form
and in the forms.py I defined the following form:
class WirelessScanForm(forms.ModelForm): time = forms.DateTimeField(label="Schedule Time", widget=AdminSplitDateTime()) def __init__(self,*args,**kwargs): myClient = kwargs.pop("client") # client is the parameter passed from views.py super(WirelessScanForm, self).__init__(*args,**kwargs) prob = forms.ChoiceField(label="Sniffer", choices=[ x.sniffer.plug_ip for x in Sniffer.objects.filter(client = myClient) ])
But django keeps giving me error saying:
TemplateSyntaxError: Caught NameError while rendering: name 'myClient' is not defined
(This error happens in the query)I'm afraid it would be something stupid missing here, but I cannot really figure out why. Please help, thanks.
解决方案Assuming I've corrected your formatting properly, you have an indentation issue:
prob
is outside__init__
, so doesn't have access to the localmyClient
variable.However if you bring it inside the method, it still won't work, as there are two other issues: first, simply assigning a field to a variable won't set it on the form; and second, the
choices
attribute needs a list of 2-tuples, not just a flat list. What you need is this:def __init__(self,*args,**kwargs): myClient = kwargs.pop("client") # client is the parameter passed from views.py super(WirelessScanForm, self).__init__(*args,**kwargs) self.fields['prob'] = forms.ChoiceField(label="Sniffer", choices=[(x.plug_ip, x.MY_DESCRIPTIVE_FIELD) for x in Sniffer.objects.filter(client = myClient)])
Obviously replace
MY_DESCRIPTIVE_FIELD
with the actual field you want displayed in the choices.这篇关于django形式:将参数从view.py传递给表单发出错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!