Django在Admin页面上显示模型图像

时间:2018-06-04 13:19:40

标签: django django-models django-admin imagefield

如何在管理页面中的该模型的详细信息页面上添加属于此模型的图像?图像可以位于数据/文本的右侧,也可以位于数据/文本/

的下方
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})

1 个答案:

答案 0 :(得分:-1)

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', )