簡體   English   中英

Django在管理員列表中上傳文件

[英]Django uploaded file in admin list

我在這里閱讀了有關此問題的幾篇文章,並嘗試了許多方法-最后,我返回了Django文檔。

我正在嘗試將上傳的圖片顯示在管理員列表中...

我的models.py(基於來自此處的信息: Django Docs

from django.db import models
from django.contrib import admin
from django.utils.html import format_html

class File(models.Model):
    file = models.ImageField()
    description = models.CharField(max_length=200)
    def __str__(self):
        return self.description
    def image_tag(self):
        return format_html(
            '<img src="{}" />', self.file
        )

class FileAdmin(admin.ModelAdmin):
    list_display = ('image_tag', 'description')    

不幸的是,這無濟於事...

我也嘗試放置任意HTML-以防萬一文件url錯誤...但沒有任何變化。

任何想法都將不勝感激-現在對此頗為沮喪。

您也可以嘗試使用format_html代替format_html

如下更新models.py

from django.utils.safestring import mark_safe

def image_tag(self):

    if self.file:
        return mark_safe('<img src="%s" width="200" height="200"/>' % self.file)
    else:
        return '(No image found)'

image_tag.short_description = 'Thumb'

在您的admin.py添加:

list_display= ('image_tag',)

還有一個軟件包django-imagekit可能會有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM