繁体   English   中英

在两个日期之间搜索(如果存在) - django - ajax

[英]search between two dates, if exists - django - ajax

我正在尝试通过 ajax 响应将两个日期从模板发送到后端,以检查这两个日期是否存在,如果不存在则返回整个数据,但如果存在则返回这两个范围之间的数据

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')

这是我的 GET 表格,用于获取两个日期

 $(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>

我必须检查输入日期是否存在然后将data变量调用到 ajax,但现在即使我没有任何搜索仍然假装日期存在并从服务器端返回此错误

django.core.exceptions.ValidationError: ['“无效日期”值的格式无效。 它必须是 YYYY-MM-DD HH:MM[:ss[.uuuuuu]][TZ] 格式。'] 非常感谢您的帮助..

我认为您需要转换 GET 参数中的值

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

首先是str/None到 datetime 对象。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM