【问题标题】:Django show model images on Admin pageDjango 在管理页面上显示模型图像
【发布时间】:2018-11-13 20:25:06
【问题描述】:

如何在管理页面的该模型的详细信息页面上添加属于该模型的图像?图片可以在数据/文本的右侧或数据/文本的下方/

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})

【问题讨论】:

标签: django django-models django-admin imagefield


【解决方案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', )

【讨论】:

  • 虽然这段代码 sn-p 可以解决问题,但它没有解释为什么或如何回答这个问题。请include an explanation for your code,因为这确实有助于提高您的帖子质量。请记住,您是在为将来的读者回答问题,而这些人可能不知道您提出代码建议的原因。
猜你喜欢
  • 1970-01-01
  • 2020-09-30
  • 2015-10-25
  • 2011-01-27
  • 2017-06-21
  • 2011-05-09
  • 2015-05-17
  • 1970-01-01
  • 2023-02-04
相关资源
最近更新 更多