Get form send data to backend-ajax - django

I'm trying to send two dates from template to backend through ajax response , in order to check if the two date exists or not! if not exists then return the entire data, but if exists just return the data between that two range

def dateTimePrice(request):
   start = request.GET.get('from')
   end = request.GET.get('to')
   print(start,end)#
   if start and end:
        datetimes = ImeiInvoice.objects.filter(invoice__created_at__range=(start,end)).annotate(
            total_price=Sum(
                (F('price')) - F('discount'),output_field=DecimalField(max_digits=20,decimal_places=3))
        ).annotate(
            total_quantity=(
                Count('pk')
            )
        ).aggregate(
            all_price=Sum(F('total_price')),
            all_qnt=Sum(F('total_quantity'))
        )
    
   else:
        datetimes = ImeiInvoice.objects.all().annotate(
            total_price=Sum(
                (F('price')) - F('discount'),output_field=DecimalField(max_digits=20,decimal_places=3))
        ).annotate(
            total_quantity=(
                Count('pk')
            )
        ).aggregate(
            all_price=Sum(F('total_price')),
            all_qnt=Sum(F('total_quantity'))
        )
return JsonResponse(datetimes,safe=False)

@login_required
def dateTimePriceTemp(request):
    return render(request,'myapp/datetimes.html')

and here is my GET form, to get the two date from

$(document).ready(function(){
    const spinner = document.getElementById('spinnerDateTimePrice')
    const start_date = new Date($('#from').val());
    const end_date = new Date($('#to').val());
    console.log(start_date)
    console.log(end_date)
    if(start_date && end_date){
        data={
        'from':start_date,
        'to':end_date            
        }
    }
        function dateTimePrices(){        
            $.ajax({
                type:'GET',
                url:'/prices/dateTime/data',
                data:data,                
                success:function(data){
                    const datetimes = data;
                    var k = '<tbody>';
                    if(datetimes){
                        k+= '<tr>';
                            k+= '<td>' + datetimes["all_qnt"] + '</td>';
                            k+= '<td>' + datetimes['all_price'] + '</td>';

                        k+= '</tr>'                    
                    }else{
                        k+= '<td class="p-2 text-xs border border-purple-900 md:text-base textpurple" colspan=6>found nothing</td>'
                    }
                    k+='</tbody>'
                    document.getElementById('datetime_list').innerHTML = k        
                }                
            })        
        }    
        dateTimePrices();    
})
    <form action="" method="GET">

        <div class="col-11 p-1 mt-1 mx-auto text-center row rtl ">
            <p class="col-12 col-sm-6 mx-auto text-left row">
            <img src="icons/date.svg" class="ml-1" alt="">  
            from  
            <input type="date" class="form-control col-9 mr-1" name="from" id="from"> 
            </p> 
            
            <p class="col-12 col-sm-6 mx-auto text-right row">
                <img src="icons/date.svg" class="ml-1" alt="">  
                to
                <input type="date" name="to" class="form-control col-9 mr-1" id="to">   
            </p>
            <button class="btn btn-info col-8 col-sm-5 col-md-3 mx-auto">search</button>
        </div> 
    </form>

i have to check if input dates exists then call the data variable into the ajax, but now even if i didnt any search still pretend like the date exists and return this error from server side

django.core.exceptions.ValidationError: ['“Invalid Date” value has an invalid format. It must be in YYYY-MM-DD HH:MM[:ss[.uuuuuu]][TZ] format.'] i much appreciate your helps ..

i think you need to convert the values from your GET parameters

start = request.GET.get('from')
end = request.GET.get('to')

which are either str/None into datetime objects first.

Back to Top