Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 视图之间未销毁django列表_Python_Django_Geodjango - Fatal编程技术网

Python 视图之间未销毁django列表

Python 视图之间未销毁django列表,python,django,geodjango,Python,Django,Geodjango,我正在从一些模型数据创建一个列表,但我没有正确地执行它,它可以工作,但当我刷新broswer reportResults中的页面时,结果会被添加到。我希望它会在请求之间被垃圾收集,但显然我做错了什么,有人有什么想法吗 谢谢, 伊万 您每次都在重复使用相同的列表,为了修复它,您需要重新构造代码,以便在每个请求中创建一个新的列表。这可以通过多种方式实现,其中一种方式是: def addReportResult(reportResults, fix,description): fix.de

我正在从一些模型数据创建一个列表,但我没有正确地执行它,它可以工作,但当我刷新broswer reportResults中的页面时,结果会被添加到。我希望它会在请求之间被垃圾收集,但显然我做错了什么,有人有什么想法吗

谢谢, 伊万


您每次都在重复使用相同的列表,为了修复它,您需要重新构造代码,以便在每个请求中创建一个新的列表。这可以通过多种方式实现,其中一种方式是:

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
保持较小,您也可以通过删除对
addReportResult
的调用,并在相同位置执行
waypointsindex.description=description
来内联
description
属性集,要修复它,您需要重新构造代码,以便在每个请求上创建一个新列表。这可以通过多种方式实现,其中一种方式是:

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
保持较小,您也可以通过删除对
addReportResult
的调用并在相同位置执行
waypointsindex.description=description
来内联
description
属性集。

这样您就可以了解请求的生命周期,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})

为了让您了解请求的生命周期,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})

我假设是mod_wsgi,但我所知道的大多数Web服务器都是这样运行的。非常感谢,运行良好。也感谢你的解释。出于兴趣,我通常更喜欢camelCase,使用snake_case或personal preference有什么好处?我假设是mod_wsgi,但我知道的大多数Web服务器都是这样运行的。非常感谢,运行良好。也感谢你的解释。出于兴趣,我通常更喜欢camelCase,使用snake_case或个人偏好有什么好处?嗨,Wessie,不幸的是,我已经尝试过了,每次都以一个完全空的列表结束。在函数中创建列表的另一个答案是有效的。非常感谢你花时间来帮助我。嗨,韦斯,不幸的是我已经试过了,每次我都以一个完全空的列表结束。在函数中创建列表的另一个答案是有效的。非常感谢您抽出时间来帮忙。