views:

79

answers:

1

Hi all...

*EDITED THIS QUESTION FOR CLARITY*

I have a Django template with a flash object in it. The template itself inherits from the main template and is in a block called info. Everything works ok with that!

When the flash object is clicked it calls a JavaScript callback function, with a parameter, in a separate js file. Within this function I can load another Django template by using something like

  function myCallBack(param){
        location.href = 'crc_region_cities' // (Django url). 
  }

What I want to know is how do I pass the parameter param (which is a string) to that URL so that the view function of the same name: def crc_region_cities picks it up and does the appropriate query..

Extra info:

The new URL calls a view which renders a template, which again inherits the same main template as the previous template and resides in the same block...info.

Is there a better way to do this...?? Remember that my flash object calls a javascript callback function with a string parameter,so I have to maneuver from there somehow to the new template whilst passing the string parameter to the URL. Below in the code you can see the second parameter of the view function is called region. This is the parameter I need to pass in via javascript function.

 THE URL (r'^crc_region_cities/?$', 'crc_region_cities'),

 The View

 def crc_region_cities(request, region):
     cities = City.objects.filter(region__exact=region)
     return render_to_response('crc/crc_region_cities.html', {'cities': cities}, context_instance=RequestContext(request))

Many Thanks

A: 

It's often best not to do raw AJAX yourself because of all of the browser issues. I recommend using jQuery for this (and a whole host of other things) because it already deals with all of the weirdness out there.

Take a look at the jQuery AJAX API page, in particular look at jQuery.get() and jQuery.post() for ways of passing params.

An observation: I don't see a leading '/' on the string you are assigning to href, meaning it's relative to the originating URL for the page. This could easily cause problems. Remember, Firefox w/Firebug extension is your friend when debugging AJAX stuff. Run you page and look at the tab labeled Console. I wouldn't be surprised if it's reporting an error.

One other thing to look at: the jQuery taconite plugin is one of the sweetest AJAX extensions I've ever encountered. Check it out.

Peter Rowell