【问题标题】:django sendfile with python 3.x vs 2.7django sendfile 与 python 3.x vs 2.7
【发布时间】:2016-04-11 22:26:40
【问题描述】:

当我将 django 项目从 python 2.7 切换到 Python 3.4 时,出现以下代码错误。在 python 2.7 中一切正常。

from sendfile import sendfile
return sendfile(request, myfile, attachment=True, attachment_filename=filename)

如果我尝试运行上面的代码,我会遇到以下错误。

    return sendfile(request, myfile, attachment=True, attachment_filename=filename)
TypeError: function takes exactly 4 arguments (2 given)

我浏览了一些 python 3.4 文档并尝试了不同的其他选项。

尝试1:

        return sendfile(*(request, myfile), **{'attachment':True, 'attachment_filename':filename})
    TypeError: function takes exactly 4 arguments (2 given)

return sendfile(*(request, myfile), **{'attachment':True, 'attachment_filename':filename}

)

尝试2:

    return sendfile(request, myfile, True, filename)
TypeError: an integer is required (got type WSGIRequest)

我是 python 3.4 和 django 的新手。请告诉我上面有什么问题?

【问题讨论】:

  • 你安装了哪个sendfile包? python 还是 Django 的?
  • 另外,你为什么用*(foo,bar)而不是foo, bar**{foo:bar,baz:mumble}而不是foo=bar,baz=mumble
  • @Karthik-我正在使用 django sendfile。我已经像“从 sendfile 导入 sendfile”一样导入它。我做错了什么吗。我关注了pypi.python.org/pypi/django-sendfile。工作。
  • @ppperry- 请回答问题。我第一次尝试只使用相同的方法。这在 python 2.7 中运行良好。但是对于 Python 3.4,它不是
  • @karthikr - 默认情况下,当我使用 sendfile 时,我也安装了 django-sendfile,它使用的是 pysendfile 中定义的 sendfile 函数。如何避免它。

标签: python django python-2.7 python-3.x


【解决方案1】:

正如 cmets 推断的那样,如果您安装了 pysendfile 包而不是 django-sendfile 包,则会收到此错误。

  1. 如果您有虚拟环境,请输入您的虚拟环境。
  2. 查看安装了哪些软件包:pip freeze
  3. 如果已安装 pysendfile 而未安装 django-sendfile,请运行:pip uninstall pysendfilepip install django-sendfile

【讨论】:

    猜你喜欢
    • 2013-03-03
    • 2023-03-26
    • 2011-11-09
    • 2018-01-19
    • 2015-12-23
    • 2013-02-13
    • 2023-03-31
    • 1970-01-01
    • 2016-07-27
    相关资源
    最近更新 更多