ram1
ram1

Reputation: 6470

Assign JavaScript value to Django template tag argument

How do I assign an argument value to a Django template tag, using JavaScript?

{% url path.to.some_view arg=v %}

This doesn't work:

<script>
    var v = 5;
</script>
{% url path.to.some_view arg=v %}

Upvotes: 2

Views: 797

Answers (3)

Simon
Simon

Reputation: 56

This should work:

<script>
    var v = 5;
    var url = '{% url path.to.some_view 999 %}'.replace (999, v);
</script>

Upvotes: 2

Doug-W
Doug-W

Reputation: 1328

You could do something like: < A HREF="{% reverse path.to.some_view %}&arg=" + document.v">Some_view< /A>

Upvotes: 0

Donald Stufft
Donald Stufft

Reputation: 1148

You can't.

By the time the browser has the HTML, and the javascript is being executed the django template has already been compiled serverside and the resulting html is being displayed by your browser.

Upvotes: 6

Related Questions