-2

I've got something like this in my Django template:

<script>
var selected_direction = $("#id_direction").val();
{% for bus_stop in bus_stops %}

     {% if bus_stop.direction == selected_direction %}
        // Do something //
     {% endif %}

{% endfor %}
</script>

I can do everything with bus_stops until it reaches the {% if %} statement. How can i compare javascript variable with django variable?

/Edit

Maybe the question was not constructed properly. However i solved my problem by doing this:

<script>
var selected_direction = $("#id_direction").val();
var temp_direction;
{% for bus_stop in bus_stops %}
    temp_direction = "{{ stop.direction }}";
     if (temp_direction == selected_direction){
        ////////
        ///////
     }
{% endfor %}
</script>
Daniel Kusy
  • 322
  • 1
  • 3
  • 16

2 Answers2

2

you can compare this condition

{% if bus_stop.direction == selected_direction %}

by converting you template value to js value, and compare it in js if condition as follows.

if(selected_direction == '{{bus_stop.direction}}')
{
  //code here
}
Addden
  • 55
  • 3
1

Here is a post that explains how they work together. There is a injection risk involved with this. Maybe make an ajax call for your data and do everything in javscript.

Taylor
  • 1,223
  • 1
  • 15
  • 30