Reputation: 5531
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.
Upvotes: 0
Views: 932
Reputation: 4806
{% 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
Upvotes: 1