Django错误:需要Tuple或struct_time参数

时间:2016-02-02 08:26:24

标签: javascript python json django django-views

我正在处理一个获取2个变量showdate和viewtype的代码。可变数据通过POST方法通过JavaScript发送。

viewtype = send an srt

showdate = send a date from javascript

在这段代码中,我手动定义了varibles,因为最后的操作是查询数据库,只返回"事件"从以JSON中的showdate和viewtape生成的参数开始,作为下一个:

{"events": [["19", "Dinner," "02 \ / 02 \ / 2016 9:00" "02 \ / 02 \ / 2016 

16:30", "0", 0,0, null , 1, null, ""], ["20", "Meeting", "02 \ / 03 \ / 

2016 18:30" "02 \ / 03 \ / 2016 19:30", "0", 0, 0, "6", 1, "LOL", ""]], 

"issort": true, "start": "02 \ / 01 \ / 2016 00:00", "end": "02 \ / 07 \ 

/ 2016 23:59 "," error ": null}

在这个例子中,我们看到JSON在" events"中包含2个事件。 然后我显示错误条件和我的代码: 我希望你能帮助我,谢谢。

   Traceback:

 File "/home/zalar1/virtualenvs/go/lib/python3.4/site-packages/django 

 /core/handlers/base.py" in get_response

149. response = self.process_exception_by_middleware(e, request)

File "/home/zalar1/virtualenvs/go/lib/python3.4/site-packages/django

/core/handlers/base.py" in get_response
147.response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/home/zalar1/virtualenvs/go/Project1/Apps/Calendario/views.py"      
in loadActivities
123.     ldac = listCalendar(showdate, viewtype)

File "/home/zalar1/virtualenvs/go/Project1/Apps/Calendario/views.py"
in listCalendar
71.         calc1 = int(time.strftime("%d", pytTime))

Exception Type: TypeError at /agytno/LODT
Exception Value: Tuple or struct_time argument required

views.py

def listCalendar(day, Dtype):
    pytTime = jsToPythonTime(day)

    if Dtype == "month":
        st = time.mktime(
            0, 0, 0, time.strftime("%m", pytTime),
            1, time.strftime("%Y", pytTime)
                        )

        et = time.mktime(
            0, 0, -1, time.strftime("%m", pytTime)+1,
            1, time.strftime("%Y", pytTime)
                        )

    elif Dtype == "week":
        calc1 = int(time.strftime("%d", pytTime))
        calc2 = int(time.strftime("%w", pytTime))

        if calc2 == 0:
            calc2 = 7

        monday = srt(calc1 - calc2 + 1)

        #  suppose first day of a week is monday

        st = time.mktime(
            0, 0, 0, time.strftime("%m", pytTime),
            monday, time.strftime("%Y", pytTime)
            )

        et = time.mktime(
            0, 0, -1, time.strftime("%m", pytTime),
            monday+7, time.strftime("%Y", pytTime)
            )

    elif Dtype == "day":
        st = time.mktime(
            0, 0, 0, time.strftime("%m", pytTime),
            time.strftime("%d", pytTime),
            time.strftime("%Y", pytTime)
            )
        et = time.mktime(
            0, 0, -1, time.strftime("%m", pytTime),
            time.strftime("%d", pytTime)+1,
            time.strftime("%Y", pytTime)
            )

    return listCalendarByRange(st, et)

tiempoConv.py

 def jsToPythonTime(jsDate):

     matches = re.findall('(\d+)/(\d+)/(\d+)\s+(\d+):(\d+)', jsDate)
     matches2 = re.findall('(\d+)/(\d+)/(\d+)', jsDate)
     if matches == 1:
         ret = time.mktime(
                 matches[4], matches[5], 0,
                 matches[1], matches[2], matches[3]
                         )
         return ret

     elif matches2 == 2:             
         ret = time.mktime(0, 0, 0, matches2[1], matches2[2], matches2[3])
         return ret

1 个答案:

答案 0 :(得分:1)

您的代码存在多个问题,最初导致问题的是您的jsToPythonTime方法未返回任何内容。

re.findall返回匹配项,因此它永远不会等于整数,因此它永远不会输入if / elif语句。

time.mktime需要一个长度为9的元组的单个参数。

然后你继续将你认为的时间转换为字符串然后再转换为一个让我感到困惑的整数。