Reputation: 350
i variable type table in javascript , and i need to make a test with if
condition between this variable and a variable from django database but it' doesn't work.
the example :
var test_date = "2017-06-23";
var locations = [
{% for v in vs %}
{% if v.date == test_date %}
['okok',{{ v.latitude }},{{ v.longitude}}],
{% endif %}
{% endfor%}
]
the first example doesn't work and i don't know why.
other example :
var test_date = "2017-06-23";
var locations = [
{% for v in vs %}
{% if v.date == "2017-06-23" %}
['okok',{{ v.latitude }},{{ v.longitude}}],
{% endif %}
{% endfor%}
]
when i put the value of the variable in the test it's working
Upvotes: 0
Views: 3296
Reputation: 687
Django part of the template( {% %}
) is executed on the server, when javascript part is executed on the client side (just in the browser). All the variables inside template tags are django's variables. The point is that django sees text and template tags. It won't execute any line of jacascript.
let me show how django and how javascript sees your first example.
django:
some text
{% for v in vs %}
{% if v.date == test_date %}
another text
{% endif %}
{% endfor%}
more text
Now you see that there isn't any test_date
variable. If there is no test_date
variable (it's empty), then it coudn't be equal to v.date
.
javascript:
var test_date = "2017-06-23";
var locations = [
]
As you see (you can check it in dev tools of browser) your server didn't produce any text for javascript, so the locations
array is empty.
If you simply want to test if v.date
is equal to some string than you can create variable test_date in view corresponding to this template and add it to the context.
Upvotes: 5