1👍
You can pass both parameters and work with:
def mostraDate(request):
if request.method == 'POST' and 'fromDate' in request.POST and 'toDate' in request.POST:
displaydate = Sms.objects.raw(
'SELECT process_id, date_received,originator_msisdn, error_code'
'FROM sms'
'WHERE date_replyed BETWEEN %s AND %s',
[request.POST['fromDate'], request.POST['toDate']]
)
else:
displaydate = Sms.objects.order_by('-date_received')
paginator = Paginator(displaydate, 5)
page = request.GET.get('page')
displaydate = paginator.get_page(page)
return render(request, 'mostraDate.html', {'data': displaydate})
But there is no need to use a raw query. In fact by doing this you lose the power to order, filter, and pagiante this further. You can filter with:
def mostraDate(request):
if request.method == 'POST' and 'fromDate' in request.POST and 'toDate' in request.POST:
displaydate = Sms.objects.filter(date_replyed__range=(request.POST['fromDate'], request.POST['toDate']))
else:
displaydate = Sms.objects.order_by('-date_received')
paginator = Paginator(displaydate, 5)
page = request.GET.get('page')
displaydate = paginator.get_page(page)
return render(request, 'mostraDate.html', {'data': displaydate})
Typically you filter however with a GET request, a POST request is normally used to make state changes like logging in/out, creating/updating/deleting an entity, etc. A GET request is usually used to retrieve data. This can be all data, or a filtered variant. A GET request is thus more appropriate here.
Source:stackexchange.com