Search code examples
djangodjango-modelsdjango-adminimagefield

Django show model images on Admin page


How would I add the image belonging to this model on the detail page of that model in the Admin Page? The image can be on the right side of the data/text or below the data/text/

from django.db import models


# Create your models here.

class Gallery3(models.Model):
    class Meta:
        verbose_name = "Vintago Upload"

    CATEGORIES = (
    ('Meubels', 'Meubels'),
    ('Vazen', 'Vazen'),
    ('Schilderijen', 'Schilderijen'),
)
    title = models.CharField(max_length=100)
    category = models.CharField(max_length=10, choices=CATEGORIES, default='Meubels')
    description = models.CharField(max_length=255, blank=True)
    document = models.ImageField(upload_to='images',null=True)
    price= models.DecimalField(max_digits=10, decimal_places=2)
    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)

    def __str__(self):
        return self.title


    #def get_absolute_url(self):
     #   return reverse('gallery_detail', kwargs={'pk': self.pk})

Solution

  • def image_img(self):
        if self.image:
            return marksafe('<img src="%s" />' % self.document)
        else:
            return '(no image)'
    
    image_img.short_description = 'Thumb'
    
    # and in your admin.py add:
    list_display= ('image_img','product',)
    readonly_fields = ('image_img',)
    
    # and for adding it in the 'Edit mode' of your admin panel in your admin.py add:
    fields = ( 'image_img', )