tags:

views:

148

answers:

2

Can anyone point to an example written in Python (django preferred) with ajax for cascading forms? Cascading Forms is basically forms whose field values change if and when another field value changes. Example Choose Country, and then States will change...

+3  A: 

This is (mostly) front-end stuff.

As you may have noticed Django attempts to leave all the AJAX stuff up to you, so I don't think you'll find anything built in to do this.

However, using JS (which is what you'll have to do in order to do this without submitting a billion forms manually), you could easily have a django-base view your JS could communicate with:

def get_states(request, country):
    # work out which states are available
    #import simplesjon as sj
    return sj....

Then bind your AJAX request to the onchange event of the select (I can't remember if that's right for select boxes) and populate the next field based on the return of the JSON query.

10 minute job with jquery and simplejson.

Oli
+1: This is almost exclusively Ajax and Javascript. Almost no Django at all.
S.Lott
Agreed! Was looking for an example
+1  A: 

I would also suggest considering getting a mapping of all data once instead of requesting subfield values one by one. Unless the subfield choices change frequently (states/cities change?) or huge in numbers (>1000) this should offer best performance and it is less complex.

You don't even need to create a seperate view, just include a chunk of JavaScript (a JSON mapping more precisely) with your response containing the form.

muhuk
Depends on how much you're covering. 100 countries with 100 states would come to 200k at 20bytes/record. That's probably too much to want to push out with your form. Especially if you have a lot of users.
Oli