我在 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>
因此,当下载开始时,它会失败并出现错误 Failed - No file (file exists)
同样重要的是,如果我访问管理员并检查上传的文件并打开它们,页面显示未找到
http://localhost:8000/media/provider_files/2021/02/24/Catalogo_2021_R0oiQHD.png