I'm working in a project which I want display many images in a website, so the user can download them. I can display the name of the images, but the download isn't working. I think it's something on my views, because I'm using Django, and getting the error the current path ... didn't match any of this
. The name of the directory with the images is Images
, and has others sub directories inside, and inside those sub directories, there are the images.
My views page, I think the problem is here, in the download function:
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
My app urls, catalog.urls:
from django.urls import path, include
from .views import *
urlpatterns = [
path('', index, name='index'),
path('Images/<str:path>', downloadImage, name='download'),
]
My project urls, 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')),
]
My 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>
The stack trace:
[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:\Users\Documentos\SiteSmu4_env\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
response = get_response(request)
File "C:\Users\Documentos\SiteSmu4_env\lib\site-packages\django\core\handlers\base.py", line 179, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\Documentos\SiteSmu4_env\SiteSmu4Img\catalog\views.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:\Users\Documentos\SiteSmu4_env\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
response = get_response(request)
File "C:\Users\Documentos\SiteSmu4_env\lib\site-packages\django\core\handlers\base.py", line 179, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\Documentos\SiteSmu4_env\SiteSmu4Img\catalog\views.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:\Users\Documentos\SiteSmu4_env\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
response = get_response(request)
File "C:\Users\Documentos\SiteSmu4_env\lib\site-packages\django\core\handlers\base.py", line 179, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\Documentos\SiteSmu4_env\SiteSmu4Img\catalog\views.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