Youssef
Youssef

Reputation: 169

Django redirect does not redirect

I am a beginner with Django transaction I use this code to manage my transaction manually :

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

All is Ok, but the redirect doesn't work when my data is saved, it always returns to the same page.

Upvotes: 0

Views: 1041

Answers (1)

Alasdair
Alasdair

Reputation: 309089

Your view isn't returning the response when you call the redirect function. So your view continues and you get the default response at the end of the view.

You need to do the following in your view:

return redirect("/inputs/listdelivriedsupplierrequest/")

Upvotes: 2

Related Questions