How to pass JavaScript variables to Django template tags and filters?

I could pass Hello to with tag's dj_val and upper filter in <script></script> in index.html, then Hello and HELLO was displayed on console as shown below:

{% "index.html" %}

<script>
{% with dj_val="Hello" %}
    console.log("{{ dj_val }}") # Hello
{% endwith %}

console.log("{{ "Hello"|upper }}") # HELLO
</script>

But, I could not pass JavaScript's js_val set Hello to with tag's dj_val and upper filter in <script></script> in index.html, then nothing was displayed on console as shown below:

{% "index.html" %}

<script>
let js_val = "Hello"

{% with dj_val=js_val %}
    console.log("{{ dj_val }}") # Nothing
{% endwith %}

console.log("{{ js_val|upper }}") # Nothing
</script>

So, how can I pass JavaScript's js_val set Hello to with tag's dj_val and upper filter to display Hello and HELLO on console?

Upvotes: 0

Views: 57

Answers (0)

Related Questions