models.py
# Customer Model
class Customer(models.Model):
name =models.CharField(max_length=100)
primary_contact = models.CharField(max_length=10)
secondary_contact = models.CharField(max_length=10, blank=True)
address = models.TextField(blank=True)
def __str__(self):
return self.name
def get_absolute_url(self):
return reverse('customers')
# Issue Model
class Issue(models.Model):
date_time = models.DateTimeField(default=datetime.now)
date = models.DateField(auto_now_add = True)
customer = models.ForeignKey(Customer, on_delete = models.CASCADE)
product = models.ForeignKey(Product, on_delete=models.CASCADE, )
quantity = models.PositiveIntegerField()
kt = models.IntegerField()
rate = models.FloatField()
vat = models.IntegerField(default=15)
@property
def kt_21(self):
return round(self.kt / 21 * self.quantity , 2)
@property
def vat_amt(self):
return self.quantity * self.rate * self.vat/100
@property
def total_amt(self):
return self.quantity * self.rate + self.vat_amt
def get_absolute_url(self):
return reverse('issue-hist')
def __str__(self):
return str(self.customer) + " - "+ str(self.total_amt)+' SAR'
# Receive Model
class Receive(models.Model):
date_time = models.DateTimeField(default=datetime.now)
date = models.DateField(auto_now_add = True)
customer = models.ForeignKey(Customer, on_delete = models.CASCADE)
product = models.ForeignKey(Product, on_delete=models.CASCADE, )
quantity = models.PositiveIntegerField()
kt = models.IntegerField()
rate = models.FloatField()
vat = models.IntegerField(default=15)
@property
def kt_21(self):
return round(self.kt / 21 * self.quantity , 2)
@property
def vat_amt(self):
return self.quantity * self.rate * self.vat/100
@property
def total_amt(self):
return self.quantity * self.rate + self.vat_amt
def get_absolute_url(self):
return reverse('rec-hist')
def __str__(self):
return str(self.customer) + " - "+ str(self.total_amt)+' SAR'
我有这些模型,现在我想要的是目前的总数量发出和收到的客户和余额。为此,我试图创造一个这样的观点
views.py(This只是文件中的一个视图)
class CustomerBalanceDetailView(DetailView):
model = Customer
template_name = 'customer/balances.html'
context_object_name = 'cust'
ordering = [Lower('name')]
def balance(self, request, pk):
custo = Customer.objects.get(id = pk)
issue = Issue.objects.filter(customer = custo)
tot = issue.aggregate(iss = Sum('quantity'))
return render(request, 'customer/balances.html',context={'total':tot.iss})
现在我只尝试输出总发行量,我尝试在shell中使用这些查询,它们工作,但由于某种原因,上下文没有在模板中传递。
HTML模板
{% extends 'base2.html' %}
{% load widget_tweaks %}
{% load static%}
{% block css %}
<link rel="stylesheet" href="{% static 'utility.css' %}">
<link rel="stylesheet" href="{% static 'customer/cust_d.css' %}">
{% endblock css %}
{% block content %}
<!-- Headers -->
<h1 class="c_name h">Name</h1>
<h1 class="cont h">Amount Issued</h1>
<h1 class="s_cont h">Amount Received</h1>
<h1 class="add h">Balance </h1>
<!-- Product List -->
<div class="flex-wrap">
<div class="bar name">
<p class="p1">{{cust.name}}</p>
<p class="p2">{{total}}</p>
</div>
</div>
<!-- Update & Delete Buttons -->
{% endblock content %}
1条答案
按热度按时间wnvonmuf1#
解决方案是不使用
class based view
。只需要使用function based view
并在模板中传递上下文"dot.nottaion"
。更正的文件:
www.example.com无变更models.py
views.py
我删除了类视图,现在将不得不手动查询客户名称和所有。
HTML template