Python 根据日期计算Django数据查询
这是我的models.py文件Python 根据日期计算Django数据查询,python,django,model,views,Python,Django,Model,Views,这是我的models.py文件 class CustomerInfo(models.Model): customer_name=models.CharField('Customer Name', max_length=50) customer_mobile_no = models.CharField('Mobile No', null=True, blank=True,max_length=12) customer_price=models.IntegerField('C
class CustomerInfo(models.Model):
customer_name=models.CharField('Customer Name', max_length=50)
customer_mobile_no = models.CharField('Mobile No', null=True, blank=True,max_length=12)
customer_price=models.IntegerField('Customer Price')
customer_product_warrenty = models.CharField('Product Warrenty',null=True, blank=True,max_length=10)
customer_sell_date = models.DateTimeField('date-published')
customer_product_id=models.CharField('Product ID',max_length=150,null=True, blank=True)
customer_product_name=models.CharField('Product Name', max_length=50)
def __str__(self):
return self.customer_name
当我按日期查找数据查询信息时,我希望只按所选日期计算“客户价格”。然后我将显示到我的HTML页面。
这是我按日期进行的搜索查询
这是我的views.py文件
def dailyReport(request):
if request.method =='POST':
datelist=request.POST['datewisesearch']
try:
customers = CustomerInfo.objects.filter(
customer_sell_date__day=datelist)
months=timezone.now().month
years = timezone.now().year
return render(request, 'shop/report.html', {"customers": customers, "datelist": datelist, "months": months, "years": years})
except:
pass
return render(request,'shop/report.html', context=None)
{% extends "shop/base.html" %}
{% block content_area %}
<div class="col-lg-12">
<div class="daily_title">
<h2>Daily Report</h2>
</div>
<form action="{% url 'shop:dailyReport' %}" class="" method="POST">
{% csrf_token %}
<div class="form-group">
<label for="datewisesearch">Search Product:</label>
<input type="text" class="form-control" id="datewisesearch" name="datewisesearch">
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
</div>
<div class="col-lg-12">
<br>
<hr>
<button type="button" class="btn btn-success">See Today Date is: {{datelist}}-{{months}}-{{years}} : Reports</button>
<br>
<hr>
<div class="table table-bordered">
<table class="table">
<thead>
<tr>
<th>No</th>
<th>Invoice ID</th>
<th>Name</th>
<th>Mobile</th>
<th>Product Name</th>
<th>Product ID</th>
<th>Warrenty</th>
<th>Customer Price</th>
</tr>
</thead>
{% for x in customers %}
<tbody>
<tr>
<td>{{ forloop.counter }}</td>
<td>{{x.id}}</td>
<td><a href="{% url "shop:customersProfile" x.id %}">{{x.customer_name}}</a></td>
<td>{{x.customer_mobile_no}}</td>
<td>{{x.customer_product_name}}</td>
<td>{{x.customer_product_id}}</td>
<td>{{x.customer_product_warrenty}} Month</td>
<td>{{x.customer_price}} TK</td>
</tr>
</tbody>
{% endfor %}
<h2>Total Cost is:{{sum_price}} </h2>
</table>
</div>
</div>
{% endblock content_area %}
这是html页面
def dailyReport(request):
if request.method =='POST':
datelist=request.POST['datewisesearch']
try:
customers = CustomerInfo.objects.filter(
customer_sell_date__day=datelist)
months=timezone.now().month
years = timezone.now().year
return render(request, 'shop/report.html', {"customers": customers, "datelist": datelist, "months": months, "years": years})
except:
pass
return render(request,'shop/report.html', context=None)
{% extends "shop/base.html" %}
{% block content_area %}
<div class="col-lg-12">
<div class="daily_title">
<h2>Daily Report</h2>
</div>
<form action="{% url 'shop:dailyReport' %}" class="" method="POST">
{% csrf_token %}
<div class="form-group">
<label for="datewisesearch">Search Product:</label>
<input type="text" class="form-control" id="datewisesearch" name="datewisesearch">
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>
</div>
<div class="col-lg-12">
<br>
<hr>
<button type="button" class="btn btn-success">See Today Date is: {{datelist}}-{{months}}-{{years}} : Reports</button>
<br>
<hr>
<div class="table table-bordered">
<table class="table">
<thead>
<tr>
<th>No</th>
<th>Invoice ID</th>
<th>Name</th>
<th>Mobile</th>
<th>Product Name</th>
<th>Product ID</th>
<th>Warrenty</th>
<th>Customer Price</th>
</tr>
</thead>
{% for x in customers %}
<tbody>
<tr>
<td>{{ forloop.counter }}</td>
<td>{{x.id}}</td>
<td><a href="{% url "shop:customersProfile" x.id %}">{{x.customer_name}}</a></td>
<td>{{x.customer_mobile_no}}</td>
<td>{{x.customer_product_name}}</td>
<td>{{x.customer_product_id}}</td>
<td>{{x.customer_product_warrenty}} Month</td>
<td>{{x.customer_price}} TK</td>
</tr>
</tbody>
{% endfor %}
<h2>Total Cost is:{{sum_price}} </h2>
</table>
</div>
</div>
{% endblock content_area %}
{%extensed“shop/base.html”%}
{%block content_area%}
日报
{%csrf_令牌%}
搜索产品:
提交
参见今天的日期是:{{datelist}-{{months}}-{{years}}:报告
不
发票ID
名称
可移动的
品名
产品ID
沃伦蒂
顾客价格
{客户%%中x的百分比}
{{forloop.counter}}
{{x.id}
{{x.customer\u mobile\u no}
{{x.customer\u product\u name}
{{x.customer\u product\u id}
{x.客户\产品\仓库}月
{{x.customer_price}}TK
{%endfor%}
总成本为:{总价}
{%endblock内容\区域%}
现在,计算我所选日期内的所有“客户价格”,并在表下显示总价。可能是您想要的汇总
在存在行后的视图中,添加总价
from django.db.models import Sum
customers = CustomerInfo.objects.filter(customer_sell_date__day=datelist)
# Add next line
sum_price = customers.aggregate(sp=Sum('customer_price')).get('sp', 0)
# Don't foget to add sum_price to your context
使用聚合和求和函数:Model.objects.Aggregate(Sum('field_name'))我如何显示我的HTML页面?更新并向该页面添加详细信息我在HTML表中显示了所有客户的价格列表。现在我想计算客户的总价格,我将在HTML表下查看。添加详细信息,请检查。谢谢。它很好用。但我必须知道它是怎么工作的?你能告诉我它是怎么工作的吗?