2016-02-02 52 views
0

我正在研究一個獲取2個變量,showdate和viewtype的代碼。通過POST方法通過JavaScript發送變量數據。Django錯誤:需要Tuple或struct_time參數

viewtype = send an srt 

showdate = send a date from javascript 

在這段代碼中,我定義手動可變因素,作爲最後的操作是查詢數據庫,僅返回,在這是基於showdate相似和viewtape在JSON作爲生成的參數開始「事件」下一個:

{"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

您應該發佈與您的問題相關的唯一代碼 – Sayse

+0

您在'listCalender'方法中'pyTime'的值是什麼?例外的原因是'pyTime'沒有'time.strftime'所需的正確格式的值。 –

+0

@Sayse你好朋友,對不起,我認爲在這種情況下,到了有代碼的末尾,我把所有的代碼放在相關的位置。 –

回答

1

有你的代碼的多個問題,最初導致問題的一個是事實,你jsToPythonTime方法沒有按沒有任何回報。

re.findall返回比賽,使之永遠等於一個整數,因此它不會進入如果/ elif的語句..

time.mktime需要長度的元組的單個參數9.

你然後去把你相信時間的東西轉換成一個字符串,然後回到一個讓我感到困惑的整數。

相關問題