简体   繁体   中英

django filter with two dates

I want to filter data from the database between the two dates that I enter into the HTML form.

my Form.html

<form action=""method="post">
          {% csrf_token %}
          <input type="date" class="form-control" name="startDate" id="startDate" required><br>
          <input type="date" class="form-control" name="endDate" id="endDate" required><br>
          <button style="margin-left:150px;"type="submit" class="btn btn-success" name="date_filter">ok</button>
        </form>

my models.py

from django.db import models
from django.contrib.auth.models import User

# Create your models here.
class TimesheetDetails(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE,related_name="timesheet",null="True")
    username = models.CharField(max_length = 30)
    date = models.DateField(max_length = 10)
    day = models.CharField(max_length = 10)
    startTime = models.CharField(max_length =10)
    endTime = models.CharField(max_length =10)
    breakTime = models.CharField(max_length=3)
    normalTime = models.CharField(max_length=10)
    overTime = models.CharField(max_length = 10)
    holidayTime = models.CharField(max_length = 10)
    weekType = models.CharField( max_length = 10)
    attendance = models.CharField( max_length = 10)
    content = models.TextField( max_length = 300)

my views.py

def view_timesheet(request):
    if request.method == "POST":
        if('startDate' and 'endDate') in request.POST:
            startDate = request.POST.get('startDate')
            print(startDate)
            print (type(startDate))
            endDate = request.POST.get('endDate')
            print(endDate)
            print (type(endDate))
            user_row_count = TimesheetDetails.objects.filter(user=request.user)
            print(user_row_count)
            #dateRange = user_row_count.filter(date__range=['startDate','endDate']).count()
            #print(dateRange)
            #context = {'dateRange': dateRange}
            #print(context)
            return redirect('/total')

This is the ouput i am getting right now.

output

2019-12-06 2019-12-28 , , ]>

but when I uncomment the following line and compare the dates I get the following error. any help will be highly appreciated.

ValidationError at /list/
["'startDate' value has an invalid date format. It must be in YYYY-MM-DD format."]
Request Method: POST
Request URL:    http://127.0.0.1:8000/list/
Django Version: 2.2.8
Exception Type: ValidationError
Exception Value:    
["'startDate' value has an invalid date format. It must be in YYYY-MM-DD format."]
Exception Location: D:\Django\TimeSheetProject\morabu\lib\site-packages\django\db\models\fields\__init__.py in to_python, line 1249
Python Executable:  D:\Django\TimeSheetProject\morabu\Scripts\python.exe
Python Version: 3.7.2
Python Path:    
['D:\\Django\\TimeSheetProject',
 'D:\\Django\\TimeSheetProject\\morabu\\Scripts\\python37.zip',
 'D:\\Django\\TimeSheetProject\\morabu\\DLLs',
 'D:\\Django\\TimeSheetProject\\morabu\\lib',
 'D:\\Django\\TimeSheetProject\\morabu\\Scripts',
 'c:\\program files\\python\\Lib',
 'c:\\program files\\python\\DLLs',
 'D:\\Django\\TimeSheetProject\\morabu',
 'D:\\Django\\TimeSheetProject\\morabu\\lib\\site-packages']
Server time:    Fri, 27 Dec 2019 15:57:13 +0900

I have also tried changing date in models.py to CharField and don't get any error but my filtered count is 0.

You can use a library like dateutil

from dateutil.parser import parse
date_string = "12/27/2019"
django_understandable_date = parse(c)

You might also consider using the datetime library

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM