0

I have a model here in Django in which an entity can have several uploaded files:

from django.db import models from model_utils.models import TimeStampedModel from .managers import 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

So then in my html I would like to access entity files and give the user links to download this files:

<td>
                <a href="{{ provider.get_provider_files_urls.0 }}" download="{{ provider.get_provider_files_urls.0 }}"> Download File</a>
</td>

So then when download starts, it fails with error Failed - No file (file exists)

just for you to see the path that is taking

Also important, if I access admin, and check uploaded files and open them, page says not found

http://localhost:8000/media/provider_files/2021/02/24/Catalogo_2021_R0oiQHD.png

Guillermo Zooby
  • 582
  • 2
  • 15
  • 32
  • 1
    Is `/2021/02/24` part intentional in the url, as i think the date's forward slashes in url could be incorrectly interpreted by django. I suggest you try dropping this from media urls. – Hemant Mar 04 '21 at 05:05
  • Hi @HemantMalik thanks. I tried removing the date in upload folders but the mistake persists. Now the file is in the root of provider_files, but still No File error. – Guillermo Zooby Mar 04 '21 at 19:01

1 Answers1

0

Adding the last line to main urls.py solved the problem:

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)
Guillermo Zooby
  • 582
  • 2
  • 15
  • 32