表单不允许编辑
我有以下形式:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
简短回答:
PlayForwardPage
是一个模型,而不是ModelForm
。这是更正后的代码,带有一些额外的样式注释。
另外,您在 PlayForwardPageForm 中做了一些不必要的事情:
Short answer:
PlayForwardPage
is a model not aModelForm
.Here's the corrected code, with some extra style comments.
Also, you're doing some unnecessary things in your PlayForwardPageForm: