Django重定向不重定向

时间:2012-02-18 21:37:30

标签: python django

我是Django事务的初学者我使用此代码手动管理我的事务:

@transaction.commit_manually
@login_required
def delivried_supplier_request(request):
    if request.method == "POST":
        if 'add_ligne' in request.POST:
            cp = request.POST.copy()
            cp['commande_fournisseur_ligne_set-TOTAL_FORMS'] = int(cp['commande_fournisseur_ligne_set-TOTAL_FORMS']) + 1
            form = CommandeFournisseurForm(request.POST)
            formset = DelivredCmdLigneFormSet(cp)
            transaction.commit()
        else:
            form = CommandeFournisseurForm(request.POST)
            formset = DelivredCmdLigneFormSet(request.POST)
            if form.is_valid() and formset.is_valid():
                try:
                    cmdFournisseur = form.save()
                    instances = formset.save(commit=False)
                    for instance in instances:
                        instance.commande_fournisseur = cmdFournisseur
                        instance.save()
                    transaction.commit()
                    redirect("/inputs/listdelivriedsupplierrequest/")
                except Exception, e:
                    print e
                    messages.add_message(request, messages.ERROR, e)
                    transaction.rollback()
    else:
        form = CommandeFournisseurForm()
        formset = DelivredCmdLigneFormSet()
        transaction.commit()

    with transaction.commit_on_success():
        forms = [form]
        return render_to_response("inputs/delivredsupplierrequest.html", {
        "formset": formset, "forms": forms
        }, context_instance=RequestContext(request))

一切正常,但保存我的数据时重定向不起作用,它总是返回到同一页面。

1 个答案:

答案 0 :(得分:2)

调用redirect函数时,您的视图未返回响应。因此,您的视图将继续,并在视图末尾获得默认响应。

您需要在视图中执行以下操作:

return redirect("/inputs/listdelivriedsupplierrequest/")