簡體   English   中英

Django下載文件

[英]Django download files

我正在一個項目中工作,我希望在網站上顯示許多圖像,以便用戶可以下載它們。 我可以顯示圖像的名稱,但下載不起作用。 我認為這是我的觀點,因為我正在使用 Django,並且the current path ... didn't match any of this得到錯誤the current path ... didn't match any of this 包含圖像的目錄的名稱是Images ,里面有其他子目錄,在這些子目錄中,有圖像。

我的views頁面,我覺得問題出在這里,在下載功能里:

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

我的應用程序網址,catalog.urls:

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

我的項目網址,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:\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

您代碼中的問題是:
catalog.urls: path('Images/', include('catalog.urls')),

SiteSmu4Img: path('Images/<str:path>', downloadImage, name='download'),

所以,總的來說,路徑應該像Images/Images/<str:path> ,但在模板中你只有:
<img src="Images/{{ i }}">

首先更正此問題,然后在解決方案無法修復所有錯誤時顯示堆棧跟蹤。

在模板中添加另一個Images作為開頭,並在路徑中添加前導斜杠:

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

您無法直接獲取圖像或任何靜態文件。

Django 查找靜態文件夾或媒體文件夾的資源。

在靜態文件夾中添加圖像並在 settings.py 中添加STATIC_FOLDERSTATIC_URL

如果要通過網站添加圖片, MEDIA_URLsettings.py設置MEDIA_ROOTMEDIA_URL

urls.py添加一個 URL。 現在您可以通過 Django 訪問圖像。

獲取媒體文件訪問權限

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM