2

Clarification:

I really don't want to embed my js in my html. The suggestions I'm getting, like, this or that post might contain the possible answer, well in those answers, they embedded their js in html. Both my js and html is large. So embedding will surely make them messy and hard to debug

In my project there's a module where user can see details about product and add products to cart. For this he needs to specify the number of products he wants to add. It looks like this

For this I have a + and - icon. I have a js file where it increments or decrements each time + or - has been clicked.

Now, what I want is this:

A user can add products but if the total number exceeds the number of products stored in the database, he can't add products even if he click the + button. For example, if there are 4 items and he clicked 5 times then on the 5th click the amount will remain 4, won't increase.

So I need a django variable in my js file. I looked up how to do that, and I found some solutions.

Some solution says var data = '{{data|escapejs}}' this while some solution says

<script>
var myTable = {% autoescape off %}{{ table_data }}{% endautoescape %};
</script>

But I don't want to embed js on my html like this. Here is my js file:

function incrementValue()
{
    var value = parseInt(document.getElementById('quantity').value, 10);
    value = isNaN(value) ? 0 : value;
    value++;
    document.getElementById('quantity').value = value;
}

And my html, from where I am calling this js like this:

<span class="btn btn-default btn-lg btn-qty" onclick="incrementValue()">
    <span class="glyphicon glyphicon-plus" aria-hidden="true">
    </span>
</span>

<input class="btn btn-default btn-lg btn-qty" id="quantity" value="0" />

<span class="btn btn-default btn-lg btn-qty" onclick="decrementValue()">
    <span class="glyphicon glyphicon-minus" aria-hidden="true"></span>
    </span>
</span>

What is the best way to pass django variable to js or, How should I pass the django variable to my js so that there will be no vulnerability or no security issues and also I don't want to embed js in my html.

5
  • Possible duplicate of Django Template Variables and Javascript Commented Nov 3, 2017 at 20:06
  • Possible duplicate of How do I pass django context variables into javascript? Commented Nov 4, 2017 at 4:06
  • I've already viewed those answer. My question differs from those. I've mentioned that I don't want to embed js in html. But those answer contains js embedded in html Commented Nov 4, 2017 at 4:33
  • And also, according to the link, @solarissmoke, you gave, in your answer, you are sending dictionary from views.py. What I'm doing is I'm taking all the products from my ProductList model. Now, according to your answer, I've to find the quantity of products in the views.py and send it to html. Is it efficient? Commented Nov 4, 2017 at 4:38
  • @sphoenix I know this is a super old question, but do you mind sharing what did you end up doing ? Commented Jan 17, 2019 at 7:10

0

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.