Django下载文件



我正在做一个项目,我想在网站上显示许多图像,这样用户就可以下载它们。我可以显示图像的名称,但下载不起作用。我认为这是我的观点,因为我正在使用Django,并得到错误the current path ... didn't match any of this。包含图像的目录的名称为Images,其中还有其他子目录,在这些子目录中,有图像。

我的浏览页面,我认为问题就在这里,在下载功能:

from django.shortcuts import render
import os
from django.http import HttpResponse, Http404
from django.http import FileResponse
def index(request):
flPath = os.listdir('Images/')
fl4 = []
for root, dirs, files in os.walk("Images/", topdown=False):
for name in files:
fl4.append(os.path.join(root, name))
for name in dirs:
fl4.append(os.path.join(root, name))
return render(request, 'catalog/index.html', {'path': fl4})
def downloadImage(request, path):
imagePath = 'Images/'
file_path = os.path.join(imagePath, path)
if os.path.exists(file_path):
with open(file_path, 'rb') as fh:
response = HttpResponse(fh.read(), content_type='text/csv')
response['Content-Disposition'] = 'inline; filename=' + file_path
return response
raise Http404

我的应用程序网址,目录网址:

from django.urls import path, include
from .views import *
urlpatterns = [
path('', index, name='index'),
path('Images/<str:path>', downloadImage, name='download'),
]

我的项目URL,SiteSmu4Img:

from django.contrib import admin
from django.urls import path, include
from django.views.generic import RedirectView
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
path('Images/', include('catalog.urls')),
path('', include('catalog.urls')),
]

我的index.html:

<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/html">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
{% if path %}
<table>
<tr>
<th>Images</th>
<th>Download</th>
</tr>
{% for i in path %}
<tr>
<td>{{ i }}</td>
<td><a href="{{ i }}">download</a></td>
</tr>
<img src="Images/{{ i }}">
{% endfor %}
</table>
{% endif %}
</body>
</html>

堆栈跟踪:

[03/Nov/2020 17:11:39] "GET / HTTP/1.1" 200 3508
Not Found: /Images/Images/Images/fisc/LOGO NOVA.png
Not Found: /Images/Images/Images/leg/Niterói.jpg
[03/Nov/2020 17:11:39] "GET /Images/Images/Images/fisc/LOGO%20NOVA.png HTTP/1.1" 404 2787
[03/Nov/2020 17:11:39] "GET /Images/Images/Images/leg/Niter%C3%B3i.jpg HTTP/1.1" 404 2783
Not Found: /Images/Images/Images/leg/thumb_drone_site@2x.png
Not Found: /Images/Images/Images/urb/teste/1461270191180.jpg
[03/Nov/2020 17:11:39] "GET /Images/Images/Images/leg/thumb_drone_site@2x.png HTTP/1.1" 404 2812
[03/Nov/2020 17:11:39] "GET /Images/Images/Images/urb/teste/1461270191180.jpg HTTP/1.1" 404 2812
Not Found: /Images/Images/Images/urb/teste/1461270191336.jpg
[03/Nov/2020 17:11:39] "GET /Images/Images/Images/urb/teste/1461270191336.jpg HTTP/1.1" 404 2812
Not Found: /Images/Images/Images/urb/teste/1461270202199.jpg
[03/Nov/2020 17:11:39] "GET /Images/Images/Images/urb/teste/1461270202199.jpg HTTP/1.1" 404 2812
Not Found: /Images/Images/Images/urb/teste
[03/Nov/2020 17:11:39] "GET /Images/Images/Images/urb/teste HTTP/1.1" 404 2758
Internal Server Error: /Images/Images/Images/urb
Traceback (most recent call last):
File "C:UsersDocumentosSiteSmu4_envlibsite-packagesdjangocorehandlersexception.py", line 47, in inner
response = get_response(request)
File "C:UsersDocumentosSiteSmu4_envlibsite-packagesdjangocorehandlersbase.py", line 179, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:UsersDocumentosSiteSmu4_envSiteSmu4Imgcatalogviews.py", line 20, in downloadImage
with open(file_path, 'rb') as fh:
PermissionError: [Errno 13] Permission denied: 'Images/urb'
Internal Server Error: /Images/Images/Images/fisc
Traceback (most recent call last):
File "C:UsersDocumentosSiteSmu4_envlibsite-packagesdjangocorehandlersexception.py", line 47, in inner
response = get_response(request)
File "C:UsersDocumentosSiteSmu4_envlibsite-packagesdjangocorehandlersbase.py", line 179, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:UsersDocumentosSiteSmu4_envSiteSmu4Imgcatalogviews.py", line 20, in downloadImage
with open(file_path, 'rb') as fh:
PermissionError: [Errno 13] Permission denied: 'Images/fisc'
[03/Nov/2020 17:11:40] "GET /Images/Images/Images/urb HTTP/1.1" 500 66812
[03/Nov/2020 17:11:40] "GET /Images/Images/Images/fisc HTTP/1.1" 500 66828
Not Found: /Images/Images/Images/urb/1461270202199.jpg
[03/Nov/2020 17:11:40] "GET /Images/Images/Images/urb/1461270202199.jpg HTTP/1.1" 404 2794
Internal Server Error: /Images/Images/Images/leg
Traceback (most recent call last):
File "C:UsersDocumentosSiteSmu4_envlibsite-packagesdjangocorehandlersexception.py", line 47, in inner
response = get_response(request)
File "C:UsersDocumentosSiteSmu4_envlibsite-packagesdjangocorehandlersbase.py", line 179, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:UsersDocumentosSiteSmu4_envSiteSmu4Imgcatalogviews.py", line 20, in downloadImage
with open(file_path, 'rb') as fh:
PermissionError: [Errno 13] Permission denied: 'Images/leg'
[03/Nov/2020 17:11:40] "GET /Images/Images/Images/leg HTTP/1.1" 500 66812
Not Found: /Images/Images/Images/urb/LOGOFOOTER@2x.jpg
[03/Nov/2020 17:11:40] "GET /Images/Images/Images/urb/LOGOFOOTER@2x.jpg HTTP/1.1" 404 2794
Not Found: /Images/Images/Images/urb/x22323604_NI-Niteroi-RJ-26-05-2010Acessibilidade-em-Niteroi-Deficientes-fi.jpg.pagespeed.ic.KEumjIi-TY.jpg
[03/Nov/2020 17:11:40] "GET /Images/Images/Images/urb/x22323604_NI-Niteroi-RJ-26-05-2010Acessibilidade-em-Niteroi-Deficientes-fi.jpg.pagespeed.ic.KEumjIi-TY.jpg HTTP/1.1" 404 3061
Not Found: /Images/leg/thumb_drone_site@2x.png
[03/Nov/2020 17:11:43] "GET /Images/leg/thumb_drone_site@2x.png HTTP/1.1" 404 2770

代码中的问题是:
catalog.urls:path('Images/', include('catalog.urls')),

站点Smu4IMg:path('Images/<str:path>', downloadImage, name='download'),

所以总的来说路径应该像CCD_,但在模板中,您只有:
<img src="Images/{{ i }}">

首先更正此问题,如果解决方案无法修复所有错误,则显示堆栈竞争。

在模板中添加另一个Images作为开头,并在路径中添加前导斜杠:

<img src="/Images/Images/{{ i }}">

您无法直接获取图像或任何静态文件。

Django查找静态文件夹或媒体文件夹的资源。

在静态文件夹中添加图像,并在设置中添加STATIC_FOLDERSTATIC_URL值.py

如果要通过网站添加图像,请在settings.py中设置MEDIA_ROOTMEDIA_URL

urls.py中添加URL。现在您可以通过Django访问图像了。

获取媒体文件访问

最新更新