Python 为什么POST请求中不存在该属性?
我正在尝试使用基于Python 为什么POST请求中不存在该属性?,python,django,Python,Django,我正在尝试使用基于forms.Form的查询来填充forms.ModelForm中的字段。不幸的是,我得到了一个AttributeError,它表明该字段不存在,我不知道为什么会这样 错误为AttributeError:'ElectionSuggestionForm'对象没有属性'PostElection' 以下是views.py: def new_post(request): if request.method == 'POST': form = NewPostForm
forms.Form
的查询来填充forms.ModelForm
中的字段。不幸的是,我得到了一个AttributeError
,它表明该字段不存在,我不知道为什么会这样
错误为AttributeError:'ElectionSuggestionForm'对象没有属性'PostElection'
以下是views.py:
def new_post(request):
if request.method == 'POST':
form = NewPostForm(request.POST)
election_form = ElectionSuggestionForm(request.user, request.POST)
if form.is_valid():
post = form.save(commit=False)
post.author = Candidate.objects.get(UserID=request.user, ElectionID=election_form.PostElection)
post.save()
return redirect('/feed/')
else:
form = NewPostForm()
election_form = ElectionSuggestionForm(request.user)
return render(request, 'campaign/new_post.html', {
"form": form,
"election_form": election_form,
})
以下是forms.py:
class ElectionSuggestionForm(forms.Form):
PostElection = forms.ModelChoiceField(queryset=None)
def __init__(self, user, *args, **kwargs):
super(ElectionSuggestionForm, self).__init__(*args, **kwargs)
print(Election.objects.all().filter(candidate__UserID=user))
self.fields['PostElection'].queryset = Election.objects.all().filter(candidate__UserID=user)
感谢要访问表单的PostElection属性的值,您必须按以下方式执行
election_form.cleaned_data['PostElection']
self.cleaned_data
是一个字典,它在调用is_valid()
方法后接收所有已清理和验证的数据
确保在election\u表单上调用is\u valid()
def new_post(request):
if request.method == 'POST':
form = NewPostForm(request.POST)
election_form = ElectionSuggestionForm(request.user, request.POST)
if form.is_valid() and election_form.is_valid():
post = form.save(commit=False)
post.author = Candidate.objects.get(
UserID=request.user,
ElectionID=election_form.cleaned_data['PostElection']
)
post.save()
return redirect('/feed/')
else:
form = NewPostForm()
election_form = ElectionSuggestionForm(request.user)
return render(request, 'campaign/new_post.html', {
"form": form,
"election_form": election_form,
})
要访问表单的PostElection属性的值,必须按以下方式进行
election_form.cleaned_data['PostElection']
self.cleaned_data
是一个字典,它在调用is_valid()
方法后接收所有已清理和验证的数据
确保在election\u表单上调用is\u valid()
def new_post(request):
if request.method == 'POST':
form = NewPostForm(request.POST)
election_form = ElectionSuggestionForm(request.user, request.POST)
if form.is_valid() and election_form.is_valid():
post = form.save(commit=False)
post.author = Candidate.objects.get(
UserID=request.user,
ElectionID=election_form.cleaned_data['PostElection']
)
post.save()
return redirect('/feed/')
else:
form = NewPostForm()
election_form = ElectionSuggestionForm(request.user)
return render(request, 'campaign/new_post.html', {
"form": form,
"election_form": election_form,
})
您可以发布错误的完整回溯吗?原因是什么?不相关,但您不应该在代码中硬编码url-在返回重定向('/feed/')
中,您应该传递视图名称而不是url。您可以发布错误的完整回溯吗?原因是什么?不相关,但是您不应该在代码中硬编码url-在返回重定向('/feed/')
中,您应该传递视图名称而不是url。postrection
是election\u form
的字段,而不是form
。而选举表单
也必须进行验证,以获得已清理的数据
。此外,这确实是获取表单数据的方法(减去上面提到的两个错误),这并不能解释election\u表单上的AttributeError。PostElection
-表单类应该具有此属性。我已尝试使用我的一个项目,如果我尝试像这样访问表单属性:form\u contact.message,我得到的错误是没有属性。在拒绝帮助之前,至少应该尝试一下这个建议。您好。我不是OP,也不是“拒绝”这里的任何内容,我只是提到您的答案部分被破坏了(仍然是ATM-您必须验证选举表格
,才能获得清理过的数据)我通过election\u form
更改form
,并且我支持我的回复原因。post election
是election\u form
的字段,而不是form
。而选举表单
也必须进行验证,以获得已清理的数据
。此外,这确实是获取表单数据的方法(减去上面提到的两个错误),这并不能解释election\u表单上的AttributeError。PostElection
-表单类应该具有此属性。我已尝试使用我的一个项目,如果我尝试像这样访问表单属性:form\u contact.message,我得到的错误是没有属性。在拒绝帮助之前,至少应该尝试一下这个建议。您好。我不是OP,也不是“拒绝”任何东西,我只是提到您的答案部分被破坏(仍然是ATM-您必须验证选举表格
才能获得清理过的数据)我通过选举表格
更改表格
,并且我已经支持我回答的原因。