Get javascript variable's value in Django url template tag Get javascript variable's value in Django url template tag django django

Get javascript variable's value in Django url template tag


I found a trick that might work under most circumstances:

var url_mask = "{% url 'someview' arg1=12345 %}".replace(/12345/, tmp.toString());

It's clean, and it doesn't break DRY principle.


Just making it clear so next readers can have a comprehensive solution. In regard to the question, this is how I used Mike Lee solution for an example case which is redirecting with Javascript to a new page with a parameter in a variable named tmp:

window.location = "{% url 'your_view_name' 1234 %}".replace(/1234/, tmp.toString());


one easy way to do this is

<div id="test" style="display: none;"> {% url 'some_url:some_sub_url'%}</div><script>  url_mask = $( "#test" ).html()</script>