Get javascript variable's value in Django url template tag
It is known that there's a DRY way of directing to an URL by using django template tag "url", say
{% url "someview" arg1=X %}
Here, I want "X" to be the value of a javascript variable, say tmp
. But the following doesn't work
<script>
...{% url "someview" arg1=tmp %}...
</script>
How should I get the value inside a 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>
var name = "{% url 'your_view_name' pk=0 %}".replace('0', name_variable);