表单不允许编辑

发布于 2024-09-28 20:57:36 字数 910 浏览 6 评论 0原文

我有以下形式:

class PlayForwardPageForm(forms.ModelForm):        
    def __init__(self, *args, **kwargs):
        super(PlayForwardPageForm, self).__init__(*args, **kwargs)

    class Meta:
        model = PlayForwardPage
        exclude = ( 'id',)

    def save(self, *args, **kwargs):     
        post = super(PlayForwardPageForm, self).save(*args, **kwargs)
        post.save()

并显示它的视图:

object = PlayForwardPage.objects.all()[0]
form = PlayForwardPageForm(instance=object)

if request.method == "POST":
    form = PlayForwardPage(data=request.POST, instance=object)
    if form.is_valid():
        form.save()
        return HttpResponseRedirect(reverse('manage_playforward',))
else:
    form = PlayForwardPageForm(instance=object)

加载页面时一切正常。但是,当我尝试使用更改后的数据保存表单时,我得到:

'data' 是此函数的无效关键字参数

任何人都可以看到任何原因或此行为吗?

I have a following form:

class PlayForwardPageForm(forms.ModelForm):        
    def __init__(self, *args, **kwargs):
        super(PlayForwardPageForm, self).__init__(*args, **kwargs)

    class Meta:
        model = PlayForwardPage
        exclude = ( 'id',)

    def save(self, *args, **kwargs):     
        post = super(PlayForwardPageForm, self).save(*args, **kwargs)
        post.save()

and view that shows it :

object = PlayForwardPage.objects.all()[0]
form = PlayForwardPageForm(instance=object)

if request.method == "POST":
    form = PlayForwardPage(data=request.POST, instance=object)
    if form.is_valid():
        form.save()
        return HttpResponseRedirect(reverse('manage_playforward',))
else:
    form = PlayForwardPageForm(instance=object)

When loading page everything works fine. But when I try to save the form with changed data I get:

'data' is an invalid keyword argument for this function

Can anyone see any reason or this behavior ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不奢求什么 2024-10-05 20:57:36

简短回答:PlayForwardPage 是一个模型,而不是 ModelForm

这是更正后的代码,带有一些额外的样式注释。

# Don't shadow built-ins (in your case "object")
play_forward_page = PlayForwardPage.objects.all()[0]
# Don't need this form declaration.  It'll always be declared below. form = PlayForwardPageForm(instance=object)

if request.method == "POST":
    # Should be a the form, not the model.
    form = PlayForwardPageForm(data=request.POST, instance=play_forward_page)
    if form.is_valid():
        form.save()
        return HttpResponseRedirect(reverse('manage_playforward',))
else:
    form = PlayForwardPageForm(instance=play_forward_page)

另外,您在 PlayForwardPageForm 中做了一些不必要的事情:

class PlayForwardPageForm(forms.ModelForm):        

# This __init__ method doesn't do anything, so it's not needed.
#    def __init__(self, *args, **kwargs):
#        super(PlayForwardPageForm, self).__init__(*args, **kwargs)

    class Meta:
        model = PlayForwardPage
        exclude = ( 'id',)

#  You don't need this since you're not doing anything special.  And in this case, saving the post twice.
#    def save(self, *args, **kwargs):     
#        post = super(PlayForwardPageForm, self).save(*args, **kwargs)
#        post.save()

Short answer: PlayForwardPage is a model not a ModelForm.

Here's the corrected code, with some extra style comments.

# Don't shadow built-ins (in your case "object")
play_forward_page = PlayForwardPage.objects.all()[0]
# Don't need this form declaration.  It'll always be declared below. form = PlayForwardPageForm(instance=object)

if request.method == "POST":
    # Should be a the form, not the model.
    form = PlayForwardPageForm(data=request.POST, instance=play_forward_page)
    if form.is_valid():
        form.save()
        return HttpResponseRedirect(reverse('manage_playforward',))
else:
    form = PlayForwardPageForm(instance=play_forward_page)

Also, you're doing some unnecessary things in your PlayForwardPageForm:

class PlayForwardPageForm(forms.ModelForm):        

# This __init__ method doesn't do anything, so it's not needed.
#    def __init__(self, *args, **kwargs):
#        super(PlayForwardPageForm, self).__init__(*args, **kwargs)

    class Meta:
        model = PlayForwardPage
        exclude = ( 'id',)

#  You don't need this since you're not doing anything special.  And in this case, saving the post twice.
#    def save(self, *args, **kwargs):     
#        post = super(PlayForwardPageForm, self).save(*args, **kwargs)
#        post.save()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文