List date filter (Django)

Asked

Viewed 1,550 times

3

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

inserir a descrição da imagem aqui

Does anyone have any example of how to make this filter in the form?

I got this here in the shell, but I don’t know how to implement 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

Then I tried to 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 that.

  • Would this filtering occur with page reload (i.e. calling the view and rendering the template again) or without reloading (i.e. via Javascript)? Your current code in the view works?

1 answer

5


You must 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)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.