2

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 %}
1
  • This isn't possible because Django's template tags are processed (on the server) before the Javascript ever does, so the tags will never know what your Javascript vars even mean. Commented Jun 30, 2011 at 18:53

3 Answers 3

6

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.

Sign up to request clarification or add additional context in comments.

Comments

2

This should work:

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.