views:

59

answers:

1

I'm trying to AJAX-ify the Django contact_form app using Django (1.4.2). I want to let Django do the heavy lifting when it comes to validation and template rendering so I can keep my templates consistent and server side. Therefore, I'm returning "partial" template renderings to inject into the DOM as the user submits the form.

It almost works; The form submits, the partial template renders and is returned, validation errors are displayed, etc. However, once the form is valid, the contact_form's view does an http redirect. I've set the success_url to redirect to just the sent message url.

I expected it to redirect on the server, return the sent message and replace the form with it. However, this only worked correctly in Chrome. In Firefox and IE the entire browser redirects to my sent message url.

Is there any way to get the redirects to only affect the AJAX response and not redirect the entire browser?

+2  A: 

This is as far not possible. FF and IE are behaving correctly as per the W3 specification.

Here's an extract of chapter 4.6.4 of the W3 XMLHttpRequest specification:

4.6.4. Infrastructure for the send() method

  • If the response is an HTTP redirect

    If the redirect does not violate security (it is same origin for instance), infinite loop precautions, and the scheme is supported, transparently follow the redirect while observing the same-origin request event rules.

A HTTP redirect basically instructs the client side to fire a brand new HTTP GET request to the given location. Your best bet is to instead do a server-side include or forward to the desired resource.

Alternatively you can also let it instead return some JSON which should notify your jQuery code in some callback function that it should re-fire a new ajaxical request on the URL as returned by the JSON.

It's however a future consideration to add a property to disable following redirects. Here's another extract:

Not in this Specification

This specification does not include the following features which are being considered for a future version of this specification:

  • Property to disable following redirects;
BalusC
So much for wrapping the views then, it looks like I'll simply have to rewrite them.
Soviut