Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

216
Views
how to get all data of price, qty from html in js and multiple it accordingly and add it into html in Django. anyone help me

I have attached the HTML on Photo Question - how to get all data of price, qty from HTML in js and multiple it accordingly and add it into HTML in Django. anyone help me.

const x = document.getElementById("qty").innerHTML;
const y = document.getElementById("price").innerHTML;
const z = document.getElementById("total");

function calculator(qty, price) {
  let lowercase = price.toLowerCase();
  const remove_price_string = lowercase.replace("only", "");
  console.log(remove_price_string);
  let total = remove_price_string * qty;
  console.log(total);
}

calculator(x, y);

enter image description here

enter image description here

enter image description here

Models.py

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I'm going to assume that each bill is a Django model object. The easiest way to do this calculation in Django is to add it as a function to your model:

# models.py
from django.db import models
import re

class Bill(models.Model):
    full_name = models.CharField(max_length=30)
    food_name = models.CharField(max_length=30)
    qty = models.IntegerField()

    # Ideally, you would represent the price using
    # a DecimalField. 
    # price = models.DecimalField(decimal_places=2)
    # only = models.BooleanField(default=False)

    # However, you said you're using a string instead.
    price = models.CharField(max_length=20)

    # If price was a DecimalField, you could do a simple
    # calculation.
    #def total(self):
    #    return self.qty * self.price

    # However, since it's a string, you'll have to
    # convert it to a whole number first.
    def total(self):
        """Computes the total bill from the quantity and price."""
        # Remove non-numeric strings like 'only'
        # from the price. Warning: this will remove
        # decimal points too, so it only works with
        # whole numbers as prices.
        price = re.sub("[^0-9]", "", self.price)

        # Convert it to a whole number so we can do
        # math on it.
        price = int(price)

        # Return the calculated result
        return self.qty * price

You can call this function in your template by typing its name, without parentheses:

<td id="total">{{ bill.total }}</td>

Please be careful about how you represent currencies. If you need to represent fractional amounts, use a DecimalField with an appropriate number of decimal places set; otherwise, use an IntegerField. You should never use a FloatField to represent currencies, as it does not always store an exact representation of a currency amount.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!