Django Inline Formset Issue (list out of range)
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(r开发者_高级运维equest.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
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
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
:
organization=user.organization_set.all()[0]
website=organization.website_set.all()[0]
You sure that's not the problem?
Either way, until we see a traceback, it's hard to tell ;)
精彩评论