【发布时间】: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