0

I am new with Django and AJAX (javascript). Finally I can send some arguments to a django view. This view render an edit form. I send the parameters and the view response the form with the info that I need, but I can't to redirect to the form rendered from the view. AJAX Fucntion:

$.ajax({
    type:"GET",
    url: "/recepcion/",
    datatype: 'json',
    data: ({i: idpro[0].innerHTML, a: ano[1].innerHTML, m: mes1[2].innerHTML }),
    success: function(data){
      window.location.href = data.redirect;
      //window.open("GET",url,true)
      }
});

django view:

def RecepcionView(request):
    idp = request.GET.get('i')
    anio = request.GET.get('a')
    mes = request.GET.get('m')
    if request.method == 'POST':
       r = DetalleRecepcion.objects.get(id_proveedor=idp,anio=anio,mes=mes)
       form = RecepcionForm(request.POST, instance=r)
       if form.is_valid():
          form.save()
          return HttpResponseRedirect('/monitor/')
    else:
       r = DetalleRecepcion.objects.get(id_proveedor=idp,anio=anio,mes=mes)
       form = RecepcionForm(instance=r)

    return render_to_response('recepcion.html',
                              {'form':form},
                              context_instance=RequestContext(request))

In the firebug consoles I can see the response and is the form with the info that I need. I don't know how to redirect to the page from the response, I am not sure if I have to add some redirect function in the django view or only in the ajax success function. I am really new with this two language and I am confused where is the error.

Thanks in advance

2
  • Why are you using Ajax at all if you want to redirect? Why not just use a standard form and return a redirect from there? Oct 2, 2014 at 17:02
  • I am not sending the parameters from a form. I have an html table with a drop down button with a couple of choices. when the user click in the choice settings I take three values Id, year and month and pass it to the view. i test my view with a simple form and works fine and redirect tot he edit form without any problem. the problem is when I try to redirect from the function.
    – Joseleg
    Oct 2, 2014 at 17:45

1 Answer 1

0

I guess from your code that you have a "regular" view (not a view whose purpose is to handle ajax data). Redirecting can be achieved in the view, you do not need ajax. Just submit the form. In your recepcion.html file write something like

some html

<form method="POST" action=""> <!-- we leave the action part blank, to send the data to the same view -->
    {{ form }} <!-- the form you are passing to the template -->
    {% csrf_token %} <!-- do not forget the token or django will return an error -->
    <input type="submit" name="submit" value="Submit"/>
</form>

some html

Btw:

2
  • Thanks for the quickly reply, but I am not taking the parameters from a form instead I take the parameters froma html table rendered with django-tables2. In my table I have a drop won button with a couple of choices, every choices must to redirect to different edits forms taking some arguments like id,year and month. If you know a better way to achieve this functionality let me know please Thanks in advance
    – Joseleg
    Oct 2, 2014 at 17:47
  • 1
    BTW, I have the form like you advice me and If is end the values from a simple html form works fine and the browser redirect me to the edit form, I think my mistake is in the way to reedirect to the response in the ajax function
    – Joseleg
    Oct 2, 2014 at 17:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.