失败 - 没有文件在 Django 中下载上传的文件
Failed - No file Downloading uploaded files in Django
我在 Django 中有一个模型,其中一个实体可以有多个上传的文件:
从 django.db 导入模型
来自 model_utils.models 导入 TimeStampedModel
从 .managers 导入 ProviderManager
class Provider(TimeStampedModel):
full_name = models.CharField('Nombre', max_length=100, unique=True)
phone1 = models.CharField("Teléfono", max_length=50, blank=True, null=True)
phone2 = models.CharField("Teléfono", max_length=50, blank=True, null=True)
email1 = models.EmailField("Email", max_length=100,
blank=True, null=True)
email2 = models.EmailField("Email", max_length=100,
blank=True, null=True)
bank_info = models.TextField(
"Info Banco", max_length=250, blank=True, null=True)
objects = ProviderManager()
class Meta:
verbose_name = "Proveedor"
verbose_name_plural = "Proveedores"
ordering = ["full_name"]
def __str__(self):
return "Nombre: "+self.full_name
def get_provider_files_names(self):
provider_files = self.provider_files.all()
file_list = []
for f in provider_files:
# print(f.file.name.split('/')[-1])
file_list.append(f.file.name.split('/')[-1])
return file_list
def get_provider_files_urls(self):
provider_files = self.provider_files.all()
file_list = []
for f in provider_files:
file_list.append(f.file.url)
return file_list
class ProviderFiles(TimeStampedModel):
file = models.FileField(upload_to="provider_files/%Y/%m/%d")
provider = models.ForeignKey(
Provider, on_delete=models.CASCADE, related_name='provider_files')
class Meta:
verbose_name = "Archivos Proveedor"
verbose_name_plural = "Archivos Proveedores"
def __str__(self):
return "Nombre Proveedor: "+self.provider.full_name
那么在我的 html 中,我想访问实体文件并为用户提供下载此文件的链接:
<td>
<a href="{{ provider.get_provider_files_urls.0 }}" download="{{ provider.get_provider_files_urls.0 }}"> Download File</a>
</td>
然后当下载开始时,它失败并显示错误失败 - 没有文件(文件存在)
同样重要的是,如果我访问管理员,检查上传的文件并打开它们,页面显示找不到
http://localhost:8000/media/provider_files/2021/02/24/Catalogo_2021_R0oiQHD.png
将最后一行添加到 main urls.py 解决了问题:
from django.contrib import admin
from django.urls import path, re_path, include
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
# users app
re_path('', include('applications.users.urls')),
re_path('', include('applications.home.urls')),
re_path('', include('applications.clients.urls')),
re_path('', include('applications.providers.urls')),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
我在 Django 中有一个模型,其中一个实体可以有多个上传的文件:
从 django.db 导入模型 来自 model_utils.models 导入 TimeStampedModel 从 .managers 导入 ProviderManager
class Provider(TimeStampedModel):
full_name = models.CharField('Nombre', max_length=100, unique=True)
phone1 = models.CharField("Teléfono", max_length=50, blank=True, null=True)
phone2 = models.CharField("Teléfono", max_length=50, blank=True, null=True)
email1 = models.EmailField("Email", max_length=100,
blank=True, null=True)
email2 = models.EmailField("Email", max_length=100,
blank=True, null=True)
bank_info = models.TextField(
"Info Banco", max_length=250, blank=True, null=True)
objects = ProviderManager()
class Meta:
verbose_name = "Proveedor"
verbose_name_plural = "Proveedores"
ordering = ["full_name"]
def __str__(self):
return "Nombre: "+self.full_name
def get_provider_files_names(self):
provider_files = self.provider_files.all()
file_list = []
for f in provider_files:
# print(f.file.name.split('/')[-1])
file_list.append(f.file.name.split('/')[-1])
return file_list
def get_provider_files_urls(self):
provider_files = self.provider_files.all()
file_list = []
for f in provider_files:
file_list.append(f.file.url)
return file_list
class ProviderFiles(TimeStampedModel):
file = models.FileField(upload_to="provider_files/%Y/%m/%d")
provider = models.ForeignKey(
Provider, on_delete=models.CASCADE, related_name='provider_files')
class Meta:
verbose_name = "Archivos Proveedor"
verbose_name_plural = "Archivos Proveedores"
def __str__(self):
return "Nombre Proveedor: "+self.provider.full_name
那么在我的 html 中,我想访问实体文件并为用户提供下载此文件的链接:
<td>
<a href="{{ provider.get_provider_files_urls.0 }}" download="{{ provider.get_provider_files_urls.0 }}"> Download File</a>
</td>
然后当下载开始时,它失败并显示错误失败 - 没有文件(文件存在)
同样重要的是,如果我访问管理员,检查上传的文件并打开它们,页面显示找不到
http://localhost:8000/media/provider_files/2021/02/24/Catalogo_2021_R0oiQHD.png
将最后一行添加到 main urls.py 解决了问题:
from django.contrib import admin
from django.urls import path, re_path, include
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
path('admin/', admin.site.urls),
# users app
re_path('', include('applications.users.urls')),
re_path('', include('applications.home.urls')),
re_path('', include('applications.clients.urls')),
re_path('', include('applications.providers.urls')),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)