Django ValidationError(日期)

时间:2016-02-10 11:16:30

标签: python html django

我的系统有一个搜索框,可以搜索并显示顺序。

我通过从开始日期到结束日期进行系统搜索或使用选择标记进行搜索来自定义搜索。

目前我按开始日期搜索,但是如果我搜索并从select标签中选择,我会收到一个错误:

[u"'' value has an invalid format. It must be in YYYY-MM-DD HH:MM[:ss[.uuuuuu]][TZ] format."]

这是我的views.py

def search_form(request):

    data = OrderItem.objects.order_by().values_list('certification_type', flat=True).distinct()
    print data

    if request.method == 'GET':
        user_search = request.GET.get('searched_item')
        print "What is searched for is : ", user_search

        selected_tag = request.GET.get('certification_type')
        print "The selected certificate type is :", selected_tag

        start_date = request.GET.get('first_date')
        print "The start date is : ", start_date

        end_date = request.GET.get('second_date')
        print "The end date is : ", end_date


        if (user_search != None and selected_tag !=None) | (user_search != None and start_date != None and end_date != None):
            items = OrderItem.objects.filter(Q(certification_type__iexact=selected_tag) | (Q(created_on__range=(start_date , end_date))) , (Q(order_type__iexact= user_search) | Q(item__iexact= user_search)
                 | Q(certification_no__iexact= user_search) | Q(client__user__email__iexact= user_search) | Q(client__phone_number__iexact= user_search) | Q(id__iexact= user_search)))


            print "items  ",  items
            return render(request, 'i/search_results.html', {'items':items, 'user_search_query':user_search, 'selected_tag_query': selected_tag, "data":data})

        else:
            return render(request, 'i/dashboard.html', {'error':True,  "data":data})

    else:
        return render(request, 'i/dashboard.html', {'error':True,  "data":data})

    return render(request, 'i/search_results.html', {'items':items, 'user_search_query':user_search, 'selected_tag_query': selected_tag, "data":data})

错误指向这行代码:

if (user_search != None and selected_tag !=None) | (user_search != None and start_date != None and end_date != None):
    items = OrderItem.objects.filter(Q(certification_type__iexact=selected_tag) | (Q(created_on__range=(start_date , end_date))) , (Q(order_type__iexact= user_search) | Q(item__iexact= user_search) | Q(certification_no__iexact= user_search) | Q(client__user__email__iexact= user_search) | Q(client__phone_number__iexact= user_search) | Q(id__iexact= user_search)))

我很困惑,不知道自己做错了什么

1 个答案:

答案 0 :(得分:0)

您应该在or条件中使用if运算符,而不是|这是一个按位运算符:

if (user_search != None and selected_tag !=None) or (user_search != None and start_date != None and end_date != None):