【问题标题】:Django error: Tuple or struct_time argument requiredDjango 错误:需要元组或 struct_time 参数
【发布时间】:2016-05-10 22:55:42
【问题描述】:

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

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 在“事件”中包含 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

【问题讨论】:

  • 您应该发布与您的问题相关的唯一代码
  • pyTime 在你的listCalender 方法中的值是多少?异常的原因是pyTime 没有time.strftime 需要的正确格式的值。
  • @Sayse 朋友你好,对不起,我想在这种情况下,到最后有代码,所有代码我都放相关。
  • 嗨@MuhammadTahir,listCalendar中pyTime的值是tiempoConv.py文件中函数jsToPythonTime()的返回值。
  • 我已经删除了大多数在你的堆栈跟踪中从未提到的方法,即使那样我也无法弄清楚你是如何到达calc1的,因为mktime需要一个长度的元组9

标签: javascript python json django django-views


【解决方案1】:

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

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-03-24
    • 2012-03-02
    • 2020-09-20
    • 1970-01-01
    • 2021-12-03
    • 1970-01-01
    • 2019-05-26
    • 1970-01-01
    相关资源
    最近更新 更多