'yield' 函数将输出流式传输到浏览器,即将值附加到响应中。
我的要求是,不是“附加”,而是有任何内置函数覆盖旧值,或者只是说不将新值附加到旧值..?
解释我的要求:
以下是我的“views.py”中的函数:
def handle_uploaded_file(f):
filename = "/media/Data/static/Data/" + f.name
uploaded = 0
perc = 0.0
filesize = f.size
destination = open(filename, 'wb+')
for chunk in f.chunks():
destination.write(chunk)
uploaded = uploaded + len(chunk)
yield(str((uploaded * 100) / filesize) + "% ")
destination.close()
yield(f.name + " (" + str(round(f.size/1024.0, 2)) + " KB) uploaded successfully.")
以下是上述函数的输出:
2% 4% 7% 9% 11% 14% 16% 18% 21% 23% 25% 28% 30% 32% 35% 37% 39% 42% 44% 46% 49% 51% 53% 56% 58% 60% 63% 65% 67% 70% 72% 74% 77% 79% 81% 84% 86% 89% 91% 93% 96% 98% 100% Butterfly.wmv (2732.16 KB) uploaded successfully.
如您所见,百分比被附加到先前传递的值中,而我想用新值覆盖旧值。
在 Django/python 中是否有针对这种行为的内置函数?或者我可以通过代码模拟吗?
提前致谢。