为什么POST请求中不存在该属性?

cbuch1800

我正在尝试forms.ModelForm使用基于forms.Form. 不幸的是,我得到一个AttributeError表明该领域不存在的消息,我不确定为什么会这样。

错误是 AttributeError: 'ElectionSuggestionForm' object has no attribute '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()方法后接收所有清理和验证的数据

请确保调用is_valid()election_form为好。

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,
    })

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

为什么对象解构中的类型不存在属性?

为什么该程序不存在段错误?

如果请求主体中不存在该属性,是否可以设置属性调解器的默认值?

为什么在Amazon SDK中请求不存在的文件的预签名URL会返回URL?

为什么不存在<<<?

为什么打字稿将联合中的属性标记为不存在?

为什么在类的构造函数中声明的属性不存在?

类型“ {}”上不存在该属性

为什么这在 Python 中有效?创建一个实例,在该类中设置一个不存在的属性,然后通过该实例打印该属性

为什么C ++中不存在引用成员的功能?

为什么JavaScript中不存在clientRight或clientBottom?

函数在postgreSQL中不存在..为什么?

为什么systemd等待`fstab`中不存在的磁盘?

为什么脚本中不存在Camera?

为什么在输出路径中不存在dll

为什么composedPath在属性不存在时会抛出异常,但在属性存在时会起作用?

该属性存在于调试器中,但在编译时不存在

为什么void {}不存在?

为什么QMediaGaplessPlaybackControl不存在?

为什么WKWebView委托不存在?

为什么地图值不存在?

为什么XMLHttpRequest responseText不存在?

错误:实际上不存在该列吗?为什么?

该属性在Typescript中的自引用接口实现中不存在

AutoMapper:如果源中不存在该属性,则保留目标值

Intellisense表示该属性在Class1和Class2中不存在

__RequestVerificationToken在Ajax POST中不存在

属性“仅”在描述中不存在

TypeScript中的类型不存在属性