gpt4 book ai didi

javascript - Django错误: Tuple or struct_time argument required

转载 作者:行者123 更新时间:2023-12-03 07:47:22 27 4
gpt4 key购买 nike

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

viewtype = send an srt

showdate = send a date from javascript

在此代码中,我手动定义变量,因为最终操作是查询数据库,仅返回基于 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

View .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

最佳答案

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

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

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

然后,您继续将您认为的时间转换为字符串,然后再转换回整数,这让我感到困惑。

关于javascript - Django错误: Tuple or struct_time argument required,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35148340/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com