Django 内联表单集问题(列表超出范围)
这应该很简单,而且我昨天已经成功了。我不知道发生了什么变化,但现在抛出了一个错误。
def game_design(request):
user=User.objects.get(pk=request.user.id)
organization=user.organization_set.all()[0]
website=organization.website_set.all()[0]
surveys=website.survey_set.all()
error=''
SurveyFormSet=inlineformset_factory(Website, Survey, extra=0, can_delete=True)
NavigationFormSet=modelformset_factory(Navigation, extra=1)
if request.method=='POST':
survey_formset=SurveyFormSet(request.POST, request.FILES, prefix="surveys")
navigation_formset=NavigationFormSet(request.POST, request.FILES, prefix="navigations")
if survey_formset.is_valid() and navigation_formset.is_valid():
survey_formset.save()
navigation_formset.save()
return HttpResponseRedirect("/rewards/")
else:
error="Please fix your errors"
survey_formset=SurveyFormSet(request.POST, request.FILES,prefix="surveys")
navigation_formset=NavigationFormSet(request.POST, request.FILES,prefix="navigations")
return render_to_response('website/game_design.html', {'website':website,'survey_formset':survey_formset, 'navigation_formset':navigation_formset, 'error':error}, context_instance=RequestContext(request))
else:
survey_formset=SurveyFormSet(instance=website,prefix="surveys")
navigation_formset=NavigationFormSet(queryset=Navigation.objects.none(),prefix="navigations")
return render_to_response("website/game_design.html", {'website':website,'survey_formset':survey_formset,'navigation_formset':navigation_formset,'error':error},context_instance=RequestContext(request))
感谢您对此的任何帮助
This should be simple and I actually had it working yesterday. I have no idea what changed but it's now throwing an error.
def game_design(request):
user=User.objects.get(pk=request.user.id)
organization=user.organization_set.all()[0]
website=organization.website_set.all()[0]
surveys=website.survey_set.all()
error=''
SurveyFormSet=inlineformset_factory(Website, Survey, extra=0, can_delete=True)
NavigationFormSet=modelformset_factory(Navigation, extra=1)
if request.method=='POST':
survey_formset=SurveyFormSet(request.POST, request.FILES, prefix="surveys")
navigation_formset=NavigationFormSet(request.POST, request.FILES, prefix="navigations")
if survey_formset.is_valid() and navigation_formset.is_valid():
survey_formset.save()
navigation_formset.save()
return HttpResponseRedirect("/rewards/")
else:
error="Please fix your errors"
survey_formset=SurveyFormSet(request.POST, request.FILES,prefix="surveys")
navigation_formset=NavigationFormSet(request.POST, request.FILES,prefix="navigations")
return render_to_response('website/game_design.html', {'website':website,'survey_formset':survey_formset, 'navigation_formset':navigation_formset, 'error':error}, context_instance=RequestContext(request))
else:
survey_formset=SurveyFormSet(instance=website,prefix="surveys")
navigation_formset=NavigationFormSet(queryset=Navigation.objects.none(),prefix="navigations")
return render_to_response("website/game_design.html", {'website':website,'survey_formset':survey_formset,'navigation_formset':navigation_formset,'error':error},context_instance=RequestContext(request))
Thanks for any help on this one
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我的问题似乎与我的表单集中不包含实例参数有关。
我应该有以下内容:
survey_formset=SurveyFormSet(request.POST, request.FILES, instance=website, prefix="surveys")
慢慢地但肯定我会学会不犯愚蠢的错误
My issue appears to have been related to not including an instance argument in my Formset.
I should have had the following:
survey_formset=SurveyFormSet(request.POST, request.FILES, instance=website, prefix="surveys")
Slowly but surely I'll learn to not make stupid mistakes
您应该始终发布完整的回溯,以便人们可以更好地提供帮助。
如果您说没有任何更改并且昨天可以正常工作,那么有一个区域不需要更改代码即可引发
IndexError
:您确定这不是问题吗?
不管怎样,在我们看到回溯之前,很难说;)
You should always post the full traceback so people can help better.
If you say nothing has changed and it was working yesterday, there's one area where code doesn't need to change to throw an
IndexError
:You sure that's not the problem?
Either way, until we see a traceback, it's hard to tell ;)