views:

213

answers:

2

Hi Guys,

I've been finding my way around Django and jQuery. I've built a basic form in Django. On clicking submit, I'm using jQuey to make an AJAX request to the sever to post my data. This bit seems to work fine and I've managed to save the data. Django returns a ValidatioError when a form is invalid. Could anyone tell me how to return this set of error messages as a response to my AJAX request so i can easily iterate through it using JS and do whatever?

I found this snippet. Looking at the JS bit (processJson) you'll see that he seems to get the error messages by extracting them from the response HTML. It seems kinda kludgy to me. Is this a best way to go about it?

My apologies for any vagueness.

Thanks in advance.

A: 

When I use front-end validation, usually the response contains chunks that you can access through dot notation (dataReturned.specificData).

Based on what and how you are returning data is the key to how to access it. The more modular you handle the data returned, the easier it is to access.

// Start ajax request to server
$.ajax({
    url: '/path_to_service',
    type: 'POST',
    data: { key: value },

    // Do something with the data
    success: function(data) {
        // Data is everything that is returned from the post
        alert(data);
        // data.message could be a piece of the entire return
        alert(data.message);
    } error: function(data) { // Handle fatal errors }
});
Calvin
Hi Calvin,What is considered to be a an error and what is a a success? Is there some value that i should return?On the server side, how can i return a list of validation errors as JSON so that I can list them out on the client side using the error function?This is the bit that confuses me.Thanks.
Mridang Agarwalla
Success is if what you send the service sends back data. On success is where you would manipulate the data and do something with it (usually updating a field or displaying a message).Error is if the service returns a fatal error (the service is broken or something is corrupt). Usually you would return false and do nothing but if the service is required for your thing to exists, you would tell the user the service is down.The JSON side of things is handled by your back-end service. Unfortunately my back-end skills are not quite there so I cannot tell you how to write the service.
Calvin
A: 

You can use my adjax library to handle this for you. Install the app somewhere on your path, link the adjax.js file and add add the following to your view:

import adjax
@adjax.adjax_response
def my_view(request):
    # prepare my_form with posted data as normal
    adjax.form(request, my_form)

Enable the form using javascript after loading the adjax.js file:

 $('form').adjaxify();

And enjoy :-)

More features here: http://adjax.hardysoftware.com.au/how/ . I'll be releasing a "1.0" version in the next week, let me know how things go. Google code project is here: http://code.google.com/p/django-adjax/

Will Hardy