Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么POST请求中不存在该属性?_Python_Django - Fatal编程技术网

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-您必须验证
选举表格
才能获得清理过的数据)我通过
选举表格
更改
表格
,并且我已经支持我回答的原因。