Question :
I have a template called listaarquivos.html
where a table with information about an object of models.py
called JOB
appears.
However, I now need to filter this table by date. I then inserted two inputs into listaarquivos.html
, datainicial
and datafinal
, and a search button.
How do I get this information from the dates entered by the user the moment he clicks the search button, to filter these values in the database and return to the screen?
This is my view:
def listaarquivos(request):
ajob = Job.objects.order_by('nome_usuario').filter(status_job = 'OK - Impresso')
return render_to_response('listaarquivos.html', context_instance=RequestContext(request,{'ajob':ajob}))
Answer :
You should put your form to make GET on the page itself:
...
<form method="GET">
...
In your view, you should check if you have any start and end date parameters for the query.
from datetime import datetime
data_inicial = request.GET.get('data_inicial')
data_final = request.GET.get('data_final')
# supondo que a data esteja no formato "%Y-%m-%d
dI = datetime.strptime(data_inicial, "%Y-%m-%d")
dF = datetime.strptime(data_final, "%Y-%m-%d")
objs = Job.objects.filter(data_initial__gte=dI,data_final__lte=dF)