首页 > 解决方案 > 在变量python中存储动态数据

问题描述

我的代码创建了一个生成pdf的函数。它可以在代码中调用超过 2 次,但在第一次它可以获取我的数据,但在第二次之后它们没有给我任何价值。我如何将我的数据存储在列表中。我正在使用 list.append 但是当第二次调用方法时,它可以给我空值 [存储在数据库中的默认值] 我的代码是:-

def GeneratePdf(request, type):
    # data for testing
    # global data
    template = get_template("employeereport/appointment.html")
    # real data
    empid = request.GET.get('empid')
    Date = request.GET.get('date')

    a = []

    a.append(Date)

    print(a)

    output:- 
    ['07/13/2020']
    [None]



    expected output-
    ['07/13/2020',None]

标签: pythonpython-3.xlistarraylist

解决方案


declare the list before using it

a = []
def function():
    a.append(1)
function()
function()
print(a)

推荐阅读