Forums

Unable to display image thumbnail in my admin panel.

Good day PA.

How do I properly display thumbnails of user uploaded files in my admin site?

I'm got an error message that the file does not exist, when I tried to preview the image.

Below are codes from my app. You can go further to take a look at my entire code.

Thank you.

--in my settings.py--

MEDIA_URL = '/media/' MEDIA_ROOT = os.path.join(BASE_DIR, 'ural/media')

---in my models.py--

class Ad_form_home(models.Model): ref = models.CharField(primary_key=True,db_index=True,max_length=200) image = models.ImageField(null=True, blank=True, upload_to="images/%Y/%m/%d/")

def __str__(self):
    return f"{self.ref}"

def image_view(self):
    return mark_safe("<img src='media/images/' width='100' height='100' />" % (self.image))

--in my admin.py--

class Ad_form_homeAdmin(admin.ModelAdmin): fieldset = [ ('Successful payments',
{'fields': ['ref']}, {'fields': ['image_view']}, {'fields': ['image']}), ]

list_display = ('ref', 'image_view', 'image')

--in my urls.py--

path(os.getenv('SECRET_ADMIN_URL') + '/admin/', admin.site.urls), ]+static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

Check what url is actually requested in the dev tools of your browser.

Hello fjl

https://www.mydomainname. .. /media/images/2022/12/10/screenshot.png

It doesn't display the acttual image.

Have you gone through the debugging steps on this help page? If not, you might find them useful in tracking down the problem.

Thank you Giles.

I followed the help page content to fix the issue.

Glad to hear that!