Укажите путь к файлу, который будет загружен в django

views.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 fh:
            response = HttpResponse(fh.read(), content_type="application/pdf")
            response['Content-Disposition'] = 'inline; filename=' + os.path.basename(file_path)
            return response
    raise Http404

urls.py

from unicodedata import name
from django.urls import path
from Profile import views

urlpatterns = [
  ...
  path('download/<str:path>/', views.download, name="download"),
]

models.py

from django.db import models
from django.contrib.auth.models import User
from taggit.managers import TaggableManager

# Create your models here.
class ProfileModel(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    bio = models.TextField(null=True)
    address = models.CharField(max_length=255 , null=True)
    image = models.ImageField(null=True, blank=True)

    def __str__(self):
        return str(self.user)

шаблон

{% extends 'base.html' %}

{% block content %}
Welcome, {{request.user.username}}
<table>
    <tr>
        <th>First Name: </th>
        <th>{{user.first_name}}</th>
    </tr>
    <tr>
        <th>Last Name: </th>
        <th>{{user.last_name}}</th>
    </tr>
    <tr>
        <th>Email: </th>
        <th>{{user.email}}</th>
    </tr>
    <tr>
        <th>Image: </th>
        {% if user.image %}
        <!-- here is the error -->
        <th><a href="{% url 'download' user.image.url%}"><img src=" {{user.image.url}}" alt="IMage...." style="object-fit: cover;
            border-radius: 50%;
            height: 200px;
            width: 200px;"></a></th>
        {% endif %}
    </tr>
    <tr>
        <th>Address: </th>
        <th>{{user.address}}</th>
    </tr>
    <tr>
        <th>Bio: </th>
        <th>{{user.bio|linebreaks}}</th>
    </tr>
</table>

MEDIA_URL = '/images/'

MEDIA_ROOT = os.path.join(BASE_DIR, 'static/images')

Проблема заключается в пути, который я указал для загружаемого файла в шаблоне. Я не знаю, как правильно указать требуемый путь к файлу в представлении загрузки. Ошибка - "Reverse for 'download' with arguments '('/images/Nitro_Wallpaper_5000x2813_Vm2jvWw.jpg',)' not found. Проверен 1 шаблон(ы): ['Profile/download/(?P[^/]+)/\Z']". Но "Nitro_Wallpaper_5000x2813_Vm2jvWw.jpg" присутствует в моей папке images.

views.py

def download(request, filename):
    path = os.path.join(settings.MEDIA_ROOT, filename)
    file_path = os.path.join(settings.BASE_DIR, path)
    if os.path.exists(file_path):
        with open(file_path, 'rb') as fh:
            response = HttpResponse(fh.read(), content_type="application/octet-stream")
            response['Content-Disposition'] = 'inline; filename=' + os.path.basename(file_path)
            return response
    raise Http404

шаблон

{% extends 'base.html' %}

{% block content %}
Welcome, {{request.user.username}}
<table>
    <tr>
        <th>First Name: </th>
        <th>{{user.first_name}}</th>
    </tr>
    <tr>
        <th>Last Name: </th>
        <th>{{user.last_name}}</th>
    </tr>
    <tr>
        <th>Email: </th>
        <th>{{user.email}}</th>
    </tr>
    <tr>
        <th>Image: </th>
        {% if user.image %}
        <th><a href="{% url 'download' user.image%}"><img src="{{user.image.url}}" alt="IMage...." style="object-fit: cover;
            border-radius: 50%;
            height: 200px;
            width: 200px;"></a></th>
        {% endif %}
    </tr>
    <tr>
        <th>Address: </th>
        <th>{{user.address}}</th>
    </tr>
    <tr>
        <th>Bio: </th>
        <th>{{user.bio|linebreaks}}</th>
    </tr>
</table>

Предоставление только имени файла в параметре функции и установка пути к файлу в представлении загрузки. Также установите тип_содержимого как octet/stream.

Вернуться на верх