什么是“产量”的替代方案? Django 中的函数

发布于 2024-10-19 20:59:52 字数 979 浏览 4 评论 0原文

“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 中是否有针对此行为的内置函数?或者我可以通过代码模拟这个吗?

提前致谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

豆芽 2024-10-26 20:59:52

您无法覆盖已发送到浏览器的内容。这是远程网络连接,请记住:一旦发送了某些内容,它就已经发送了。

您需要使用 javascript 或 CSS 进行一些巧妙的操作才能获得您想要的结果。

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.

装纯掩盖桑 2024-10-26 20:59:52

这个响应看起来很肮脏。

一般来说,大多数时候文件上传的进度条是使用 JavaScript 和 Web 服务器模块(我将其与 Rails 一起使用)或 JSON 视图(我将其与 django 一起使用)来实现的,该视图以 JSON 形式返回上传状态。以下是一些模块:

  • upload_progress_module for Apache
  • HttpUploadProgressModule for nginx
  • mod_uploadprogress for lighttpd

请注意,这些模块考虑了 Rails。但它们应该让您了解返回进度的 json 视图的界面应该是什么样子。通过一些 Java 脚本示例,您可以大致了解 JS 部分的工作方式。

This response thing looks quite dirty.

Generally speaking most of the time progress bars on file uploads are implemented using JavaScript and a web server module(I used it with Rails) or a JSON view(I used with django ) that returns the status of the upload in JSON. Here are some modules:

  • upload_progress_module for Apache
  • HttpUploadProgressModule for nginx
  • mod_uploadprogress for lighttpd

Note that these modules have Rails in mind. But they should give you an idea how the interface of your json view that returns the progress should look. And have some Java Script examples that you can get a general feeling how things should work on the JS part.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文