Django 内联表单集问题(列表超出范围)

发布于 2024-10-17 17:33:27 字数 1720 浏览 6 评论 0原文

这应该很简单,而且我昨天已经成功了。我不知道发生了什么变化,但现在抛出了一个错误。

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 技术交流群。

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

发布评论

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

评论(2

压抑⊿情绪 2024-10-24 17:33:27

我的问题似乎与我的表单集中不包含实例参数有关。

我应该有以下内容:

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

蓝眸 2024-10-24 17:33:27

您应该始终发布完整的回溯,以便人们可以更好地提供帮助。

如果您说没有任何更改并且昨天可以正常工作,那么有一个区域不需要更改代码即可引发 IndexError

organization=user.organization_set.all()[0]  
website=organization.website_set.all()[0]

您确定这不是问题吗?

不管怎样,在我们看到回溯之前,很难说;)

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 ;)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文