2013-01-19 3 views
1

모델 데이터 중 일부를 사용하여 목록을 만들지 만 올바르게 수행하지는 않습니다. 작동하지만 broswer에서 페이지를 새로 고침 할 때 reportResults가 추가됩니다. 요청간에 가비지 수집을 원했지만 분명히 나, 어떤 아이디어가 잘못 되었습니까?보기간에 django 목록이 삭제되지 않음

감사합니다, 이완

reportResults = [] #the list that doesn't get collected 
def addReportResult(fix,description): 
    fix.description = description 
    reportResults.append(fix) 

def unitHistory(request,unitid, syear, smonth, sday, shour, fyear, fmonth, fday, fhour, type=None): 
    waypoints = Fixes.objects.filter(name=(unitid)) 
    waypoints = waypoints.filter(gpstime__range=(awareStartTime, awareEndTime)).order_by('gpstime')[:1000] 
    if waypoints: 
     for index in range(len(waypoints)): 
...do stuff here selecting some waypoints and generating "description" text 
        addReportResult(waypointsindex,description) ##append the list with this, adding a text description 

    return render_to_response('unitHistory.html', {'fixes': reportResults}) 

답변

0

mod_wsgi는 요청의 수명주기를 알고 있기 때문에 여러 요청을 처리하기 위해 프로세스를 열어 둘 것입니다. 이 프로세스는 매번 재활용되지만, 귀하가 가정 한대로 단일 요청에 확실히 묶이지는 않습니다.

즉 로컬 목록이 필요합니다. addReportResult 함수 내용을 직접 인라인으로 옮기는 것이 좋지만 재사용이 필요하거나 함수가 너무 길면 좋은 생각이 아닙니다. 대신 해당 함수가 항목을 반환하도록하고 결과를 로컬로 수집 할 수 있습니다.

def create_report(fix, description): # I've changed the name to snake_casing 
    fix.description = description 
    return fix 

def unit_history(request,unitid, syear, smonth, sday, shour, fyear, fmonth, fday, fhour, type=None): 
    reports = [] 
    waypoints = Fixes.objects.filter(name=(unitid)) 
    waypoints = waypoints.filter(gpstime__range=(awareStartTime, awareEndTime)).order_by('gpstime')[:1000] 
    if waypoints: 
     for index in range(len(waypoints)): 
      report = create_report(waypointsindex, description) 
      reports.append(report) 
    return render_to_response('unitHistory.html', {'fixes': reportResults}) 
+0

나는 mod_wsgi을 사용한다고 가정하지만, 대부분의 웹 서버는 이런 식으로 행동하는 것을 알고있다. –

+0

고맙습니다. 잘했습니다. 그리고 설명도 감사합니다. 관심사에서 나는 보통 camelCase를 선호한다, 거기 snake_case 또는 개인적인 특혜 사용에 이점? – user1333095

1

당신은 같은 목록 매번 다시 사용하는, 당신은 모든 요청에 ​​새 목록을 생성하는 코드를 재구성해야하는 문제를 해결할 수 있습니다. 이것은 여러 가지 방법으로 수행 할 수 있으며, 이것은 하나의 방법은 다음 addReportResult 작은 유지

def addReportResult(reportResults, fix,description): 
    fix.description = description 
    reportResults.append(fix) 

def unitHistory(request,unitid, syear, smonth, sday, shour, fyear, fmonth, fday, fhour, type=None): 

    reportResults = [] # Here we create our local list that is recreated each request. 

    waypoints = Fixes.objects.filter(name=(unitid)) 
    waypoints = waypoints.filter(gpstime__range=(awareStartTime, awareEndTime)).order_by('gpstime')[:1000] 
    if waypoints: 
     for index in range(len(waypoints)): 
      # Do processing 
      addReportResult(reportResults, waypointsindex, description) 
      # We pass the list to the function so it can use it. 

return render_to_response('unitHistory.html', {'fixes': reportResults}) 

경우도 모두 addReportResult에 대한 호출을 제거하고 같은 위치에 waypointsindex.description = description을 수행하여 설정 description 속성을 인라인 수 있습니다.

+0

안녕하세요 Wessie, 불행히도 나는 이미 그걸 시도해 봤고 매번 완전히 빈 목록으로 끝납니다. 함수 내부에서 목록을 만드는 또 다른 대답이 작동합니다. 도와 주실 시간을 내 주셔서 대단히 감사합니다. – user1333095

관련 문제