1

I have the following model field

logo = models.ImageField(upload_to=municipality_logo_file_name)

and function that uploads the image to the desired location.

def municipality_logo_file_name(instance, filename):
    name, extension = os.path.splitext(filename)
    return os.path.join('municipality', 'logo', str(uuid.uuid4()) + extension.lower())

In the development mode, a file uploads correctly to for example /media/municipality/logo/1e3cc24a-841b-4577-aa59-b53e5b10420b.png and then displays correctly in the template using

<img src="{{ municipality.logo.url }}" alt="Logo obce" style="max-height: 8rem; width: auto">

In the production, file uploads well, but does not display in the template and the url cannot be followed to display image, the response is

Not Found
The requested resource was not found on this server.

Using IIS and wfastcgi.py.

Any hint as to why this might be happening would be much appreciated.

David Louda
  • 498
  • 5
  • 19
  • 1
    What server are you using? You have to configure the server to serve the media files, as it's not done by django in development. – Anthony Jul 08 '21 at 12:57

1 Answers1

0

Try this:

from django.conf import settings
from django.db import models

class Image(models.Model)
    image = models.ImageField('Image', upload_to='video-image/')

    @property
    def image_url(self):
        return '%s%s' % (settings.HOST, self.image.url) if self.image else ''
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
  • Please add more details to your code and the comment code should be in answer please do that too. – Sandeep Sharma Jul 09 '21 at 10:21
  • Hi, in the models I am creating image url and saving it with image if {{image.url}} method does not work, you can use this method. This method request for image url and base respose with url. On {{image.url}} method you request for image and base send you image and .url try to take image's url its not true way you can use method i wrote on top and you will sense its usefull – Abduvahob Kaxarov Jul 09 '21 at 15:08