So it's late and I may be missing the point, but it looks like you're mixing up the time of execution for some items... It seems like you're trying to set a template variable equal to the result of a json call and then subsequently use it for something. I can only think of two scenarios here:
1. You're trying to execute template logic in the original template after the user does something. This isn't possible because by the time the page has loaded, the template execution is completed 2. You're not actually trying to do this when a user event on the client side happens, but instead during the template execution (similar to Cake's "requestAction"). This would actually be sensible (though you couldn't utilize jquery as it would be mid-template-process), but, as far as I know, there isn't a super clean way of doing this in Django - the way Django folks and Rails folks think about templating is fundamentally different. Although I do have some code for this that I can always share and should probably put online sometime. But it sounds like you're in situation #1. In which case, just remember that once the page loads, you can't do anything with the original template code - it's already done executing. It sounds like you need to either return a json object and do some fun dhtml, or just return the sorted table and replace part of the page with a nifty .update - well, probably not 'update,' that's prototype syntax. Cheers, Dan On Mon, May 11, 2009 at 1:59 PM, snorkel <[email protected]> wrote: > > I have a complex page of data and want to sort various columns of the > db so... the way I am trying to do it is > with jquery but I don't know how I can get the variable returned by > the jquery function into my template > maybe I should be using custom template tage for this? > > Here is a very stripped down version > In my html file > <script type="text/javascript"> > function sort_col(col_name,seq_name) > { $.postJSON("/jobs/sequence/sort_shots", > {seq:seq_name,col:col_name}, > function (json) > { > {{shot_list}}=json['shot_list']; // this dosn't work but it > is what I would like to do > } > } > </script> > > {% for f in shot_fields %} > <td onclick='sort_col("{{f}} {{a.name}}")'> {{f}} > <ol> > {% for s in shot_list %} > <li> {{s|lookup:f}} </li> <!-- where lookup is constructing a > string like a.name etc --> > {% endfor %} > </ol> > {% endfor %} > > Here is the view function in views.py > > # ajax request for sorting of database rows > def sort_shots(request): > result={'shot_list': []} > if request.method =="POST": > try: > seq=request.POST["seq"] > col=request.POST["col"] > except KeyError: > json=simplejson.dumps(result) > return HttpResponse(json,mimetype='application/json') > > s=list(Shot.objects.filter(sequence__name=seq)).order_by(col) > result={'shot_list':s} > if len(s)==0: > json=simplejson.dumps(result) > else: > json=simplejson.dumps(result) > return HttpResponse(json,mimetype='application/json') > > I don't want to just reload the page because it has quite a lot of > state that would be lost > Thanks for any help > > > > --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---

