更多pdf文件保存在具有不同日期的表中,如何在fromdate和todate之间过滤pdf文件。
如何在日期之间过滤并列出pdf文件。
views.py
def select_date(request):
client = Client.objects.all()
process = Client_Process.objects.all()
pdf = Client_files.objects.all()
today = date.today()
yesterday = today - timedelta(days = 1)
print(today)
print(yesterday)
if request.method == "POST":
fromdate = request.POST.get('fromdate')
todate = request.POST.get('todate')
user = Client_files.objects.filter(Date__range=(fromdate,todate))
print(user)
return render(request,'select_date.html', {'pdf':pdf,'client':client,'process':process})
models.py
class Client_files(models.Model):
Date = models.DateTimeField(default=datetime.now, blank=True)
client = models.ForeignKey(Client, on_delete=models.CASCADE,null=True)
client_process = models.ForeignKey(Client_Process, on_delete=models.CASCADE,null=True)
File_Name = models.FileField()
Pages = models.IntegerField(null=True)
Count = models.IntegerField(null=True)
Status = models.BooleanField(default = False)
class Meta:
db_table : 'client_files'
答案 0 :(得分:2)
您应该使用__date
lookup [Django-doc]将日期时间首先转换为日期:
def select_date(request):
client = Client.objects.all()
process = Client_Process.objects.all()
pdf = Client_files.objects.all()
today = date.today()
yesterday = today - timedelta(days = 1)
print(today)
print(yesterday)
if request.method == "POST":
fromdate = request.POST.get('fromdate')
todate = request.POST.get('todate')
pdf = Client_files.objects.filter(Date__date__range=(fromdate, todate))
print(pdf)
return render(request,'select_date.html', {'pdf':pdf,'client':client,'process':process})
通常,当您过滤项目时,会在GET请求中将参数作为GET参数传递。 POST请求通常会更改数据。