The 'yield' function streams the output to the browser i.e. it appends the value to the response.
My requirement is that instead of "appending", is there any built in function which overwrites the old value, or just say does not append the new value to the old one..?
To explain my requirement:
following is the function in my "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.")
Following is the output of the above function:
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.
As you see, the percentage gets appended to the previous passed values, whereas I want to overwrite the old value with the new one.
Is there any built in function for this behaviour in Django/python? Or can I simulate this through code?
Thanks in advance.
You can't overwrite things you've already sent to the browser. This is a remote network connection, remember: once something has been sent, it's been sent.
You'll need to do something clever with javascript or CSS to get the result you want.