【问题标题】:Django: url instead of image in AdminDjango:管理中的网址而不是图片
【发布时间】:2019-06-22 04:33:21
【问题描述】:

我需要在管理员中显示我的 order_item 的图像预览(小尺寸图像)。

我基本上在这里关注这些其他问题/答案:

Django Admin Show Image from Imagefield

但是,我无法获得想要的结果。我得到了这个:

我虽然可能是 URL,但该文件的相对路径是相同的(静态部分除外):

static/media/images/python.png

可能出了什么问题?

models.py

class OrderItem(models.Model):
    order = models.ForeignKey(Order, on_delete=models.CASCADE)
    product = models.CharField(max_length= 200)
    quantity = models.CharField(max_length= 200)
    size = models.CharField(max_length=200)
    price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name= 'PEN Price')
    image = models.ImageField(upload_to='images', blank=True, null=True)
    comment = models.CharField(max_length=200, blank=True, null=True, default='')
    uploaded_at = models.DateTimeField(auto_now_add=True)



    class Meta:
        db_table = "OrderItem"

    def image_thumbnail(self):
        return u'<img src="%s" />' % (self.image.url)

    image_thumbnail.short_description = 'Image Thumbnail'
    image_thumbnail.allow_tags = True

    def sub_total(self):
        return self.quantity * self.price

admin.py

# Register your models here.

class OrderItemAdmin(admin.TabularInline):
    model = OrderItem
    fieldsets = [
        # ('Customer', {'fields': ['first_name', 'last_name'], }),
        ('Product', {'fields': ['product'],}),
        ('Quantity', {'fields': ['quantity'],}),
        ('Price', {'fields': ['price'], }),
        ('Image', {'fields': ['image'], }),
        ('Image_Thumbnail', {'fields': ['image_thumbnail'], }),
    ]
    readonly_fields = ['product', 'quantity', 'price', 'image', 'image_thumbnail']
    can_delete = False
    max_num = 0
    template = 'admin/order/tabular.html'

### Order Display ###

@admin.register(Order)
class OrderAdmin(admin.ModelAdmin):
    model = Order
    list_display = ['id', 'first_name', 'last_name', 'email', 'total', 'reason', 'created']
    list_editable = ['reason',]
    list_display_links = ('id', 'email')
    search_fields = ['token', 'shipping_department', 'email']
    readonly_fields = ['id','created']

    fieldsets = [
        ('ORDER INFORMATION', {'fields': ['id','token', 'total', 'created']}),
        # ('BILLING INFORMATION', {'fields': ['billingName', 'billingAddress1', 'billingCity', 'billingPostCode',
        #                                     'billingCountry', 'emailAddress']}),
        ('SHIPPING INFORMATION', {'fields': ['first_name', 'last_name', 'shipping_address', 'shipping_department', 'shipping_province',
                                             'shipping_district', 'shipping_address1', 'shipping_address2']}),
    ]

    inlines = [
        OrderItemAdmin,
    ]

    def has_delete_permission(self, request, obj=None):
        return False

    def has_add_permission(self, request):
        return False

【问题讨论】:

    标签: django


    【解决方案1】:

    从 Django 1.9 开始,allow_tags 已被弃用,您可以使用 mark_safe

    来自documentation

    在旧版本中,您可以在方法中添加 allow_tags 属性以防止自动转义。此属性已被弃用,因为使用 format_html()、format_html_join() 或 mark_safe() 会更安全。

    所以,试试这样:

    from django.utils.html import mark_safe
    ...
    
    def image_thumbnail(self):
        return mark_safe('<img src="%s" />' % (self.image.url))
    

    【讨论】:

      猜你喜欢
      • 2015-02-20
      • 2018-05-30
      • 1970-01-01
      • 2015-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多