Django - Getting form information

Asked

Viewed 1,636 times

3

I have a template called listaarquivos.html where a table with information of an object of the models.py called JOB.

However, now I need to filter this table by date. I then inserted two inputs in the 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}))

1 answer

1


You must put your GET form on the page itself:

...
<form method="GET">
...

In your view, you should check if you have any initial and final data_parameter to make 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)

Browser other questions tagged

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