Filter by date in list (Django)

3

Imagine that I have a list and a form to filter this list by date.

Does anyone have an example of how to do this filter in form?

I got this here in the shell, but I do not know how to implement it in the form, in the template.

$ ./manage.py shell
>>> from core.models import Job
>>> import datetime
>>> start_date=datetime.date(2015,8,1)
>>> end_date=datetime.date(2015,9,1)
>>> q=Job.objects.filter(data_job__range=(start_date,end_date))
>>> for i in q: i.nome_job, i.data_job

I tried it on views.py

p = Job.objects.all()
q = self.request.GET.get('search_box')
# buscar por job
if q is not None:
    p = p.filter(nome_job__icontains=q)
# dmin = datetime.date(2015, 8, 1)
dmin = self.request.GET.get('min_date')
dmax = self.request.GET.get('max_date')
p = p.filter(data_job__lte=dmin,data_job__gte=dmax)

I need something like this.

    
asked by anonymous 26.10.2015 / 01:15

1 answer

5

You should convert your date before the search:

from datetime import datetime

dmin = self.request.GET.get('min_date')
dmax = self.request.GET.get('max_date')
min_date = datetime.strptime(dmin, "%d/%m/%Y")
max_date = datetime.strptime(dmax, "%d/%m/%Y")

p = p.filter(data_job__gte=min_date,data_job__lte=max_date)
    
26.10.2015 / 01:27