I am using MySQL database which and try to filter data according to date but while using filter I am getting nothing.While i uncomment this line
print(searchresult.Date)
then it will show error 500.
Here is my view.py
from sales.model import Mar21
def show(request):
if request.method == 'POST':
fromdate = request.POST.get('startdate')
todate = request.POST.get('todate')
# date = Mar21.objects.only('Date')
print(fromdate)
print(todate)
searchresult = Mar21.objects.filter(date__lte = fromdate,date__gte = todate)
# print(searchresult.Date)
return render(request,'front.html',{'data':searchresult})
else:
data = Mar21.objects.all()
# print(data)
return render(request,"front.html",{'data':data})
Here is my model
from django.db import models
class Mar21(models.Model):
id = models.IntegerField(db_column='Id', blank=True, null=True) # Field name made lowercase.
date = models.DateField(db_column='Date', blank=True, null=True) # Field name made lowercase.
sales_channel = models.TextField(db_column='Sales Channel', blank=True, null=True) # Field name made lowercase. Field renamed to remove unsuitable characters.
sales_order_id = models.FloatField(db_column='Sales order ID', blank=True, null=True) # Field name made lowercase. Field renamed to remove unsuitable characters.
ship_to_name = models.TextField(db_column='Ship to name', blank=True, null=True) # Field name made lowercase. Field renamed to remove unsuitable characters.
sku = models.TextField(db_column='SKU', blank=True, null=True) # Field name made lowercase.
normal_sku = models.TextField(db_column='Normal SKU', blank=True, null=True) # Field name made lowercase. Field renamed to remove unsuitable characters.
vendor = models.TextField(db_column='Vendor', blank=True, null=True) # Field name made lowercase.
quantity = models.BigIntegerField(db_column='Quantity', blank=True, null=True) # Field name made lowercase.
line_total = models.FloatField(db_column='Line Total', blank=True, null=True) # Field name made lowercase. Field renamed to remove unsuitable characters.
channel_order_id = models.TextField(db_column='channel order id', blank=True, null=True) # Field renamed to remove unsuitable characters.
class Meta:
managed = False
db_table = 'mar21'
and here is my html code
<html>
<head>
<title>Sales Dashboard</title>
</head>
<body>
<center>
<form method="POST">
{% csrf_token %}
From :<input type="date" name="startdate"/>
To :<input type="date" name="todate"/>
<input type="submit" name="search">
<hr>
<table>
<tr>
The problem is in your filter, since the expression fromdate <= DATE and todate >= DATE a is a logical error
In that sense, I recommend you do it this way.
searchresult = Mar21.objects.filter(date__range=(from_date, to_date))
# or you can reverse your filter
searchresult = Mar21.objects.filter(date__gte=from_date, date__lte=to_date)
# or use an expression
from django.db.models import Q
searchresult = Mar21.objects.filter(Q(date__gte=from_date) & Q(date__lte=to_date))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.