Home > database >  how to send input date value from template to backend through ajax request - django
how to send input date value from template to backend through ajax request - django

Time:12-15

i've to make a query base on two dates, if the dates exists, if not just run the query without any filtration, but i dont know how send the dates input value from client side to server side , here is my views

 def priceByDateTime(request):
   start = request.GET.get('from')
   end = request.GET.get('to')
   print(start,end)#
   if start and end:
        datetimes = MyModel.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 = MyModel.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 queryTemplate(request):
    return render(request,'myapp/datetimes.html')

i know how to make the query dont sure how send the input date type values to backend

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

$(document).ready(function(){
    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  colspan=6>found nothing</td>'
                    }
                    k ='</tbody>'
                    document.getElementById('datetime_list').innerHTML = k        
                }                
            })        
        }    
        dateTimePrices();    
})
    <form action="" method="GET">

        <div >
            <p >
            from  
            <input type="date"  name="from" id="from"> 
            </p> 
            
            <p >
                to
                <input type="date" name="to"  id="to">   
            </p>
            <button >search</button>
        </div> 
    </form>
in the console shows invalid date and in the backend shows :

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 any idea, Thank you in advance ...

i also 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

CodePudding user response:

you need to add an event handler for the form submission using jquery!

$( "#target" ).submit(function( event ) {
  alert( "Handler for .submit() called." );
  event.preventDefault();
});
  • Related