0

I want to pass a variable to django template tag dynamically but I could not succeed.

<input id="country" />
<div id="button"> Button </div>

$('#button').click(function(){
    var dynamicVar=$('#country').val(); 
    var tags= {    
    source: {% url myView dynamicVar %}  /* but {% url myView 'someValue' %} works */
    select: function(event, s){
        $("#city").val(s.label);
        };
    $("#city").autocomplete(tags);
});

I am getting this NoReverseMatch error, saying the dynamicVar is not passed.

The long story: I am trying to get 2 cascaded dropdown lists. User selects a country, then City input becomes available. I want city field to be auto-completed field. Currently, without country field, it works but the query is slow. So I want to pre-filter it.

Emmet B
  • 5,341
  • 6
  • 34
  • 47

1 Answers1

1

{% url myView dynamicVar %} is rendered on the server side, so you have to:

var dynamicVar = $('#country').val(); 
var url = 'http://your_site.com/autocomplete_country/' + dynamicVar + '/'

To avoid hardcoding server name in the template, use request.get_host

Pawel Furmaniak
  • 4,648
  • 3
  • 29
  • 33
  • I guess this probably should work, I need to check when I get back to my workstation and select as the answer if it works. – Emmet B Mar 18 '13 at 10:51