当我尝试下载文件时,它在/ download /
上显示PermissionErrorviews.py
def download(request, path):
file_path = os.path.join(settings.MEDIA_ROOT, path)
if os.path.exists(file_path):
with open(file_path, 'rb') as f:
response = HttpResponse(f.read(), content_type="application/vnd.ms-excel")
response['Content-Disposition'] = 'inline; filename=' + os.path.basename(file_path)
return response
raise Http404
urls.py
re_path(r'^download/(?P<path>.*)$', views.download)
在html模板中
<a href="/download">Download</a>
我已经尝试过所有操作,例如将项目文件夹移动到另一个目录或以admin身份运行等,但是它说
Internal Server Error: /download/
Traceback (most recent call last):
File "C:\Users\Zako5\.virtualenvs\jurnal-6LxuXEYu\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
response = get_response(request)
File "C:\Users\Zako5\.virtualenvs\jurnal-6LxuXEYu\lib\site-packages\django\core\handlers\base.py", line 126, in _get_response
response = self.process_exception_by_middleware(e, request)
File "C:\Users\Zako5\.virtualenvs\jurnal-6LxuXEYu\lib\site-packages\django\core\handlers\base.py", line 124, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\Zako5\PycharmProjects\jurnal\jurnal_app\views.py", line 40, in download
with open(file_path, 'rb') as fh:
PermissionError: [Errno 13] Permission denied: 'C:\\Users\\Zako5\\PycharmProjects\\jurnal\\media/'
[27/Mar/2019 10:56:09] "GET /download/ HTTP/1.1" 500 66483
我们将不胜感激)这个问题使我烦了一个星期
答案 0 :(得分:1)
您的路径为空,因此它试图打开作为目录的MEDIA_ROOT
说您的目录结构如下:
-media root
- 1.xlsx
然后将游览html更新为
<a href="/download/1.xlsx">Download</a>
因此您的路径变量变为1.xlsx