# models.py
from django.db import models
class Item(models.Model):
name = models.CharField(max_length=100)
quantity = models.IntegerField()
price = models.DecimalField(max_digits=10, decimal_places=2)
# views.py
from django.db.models import Sum
from django.shortcuts import render
from .models import Item
def calculate_total(request):
total_quantity = Item.objects.aggregate(Sum('quantity'))['quantity__sum']
total_price = Item.objects.aggregate(Sum('price'))['price__sum']
context = {
'total_quantity': total_quantity,
'total_price': total_price,
}
return render(request, 'total.html', context)
# total.html
<!DOCTYPE html>
<html>
<head>
<title>Total Calculation</title>
</head>
<body>
<h1>Total Quantity: {{ total_quantity }}</h1>
<h1>Total Price: {{ total_price }}</h1>
</body>
</html>