【问题标题】:how to display a pdf in django admin? instead of the link如何在 django admin 中显示 pdf?而不是链接
【发布时间】:2022-11-29 04:38:39
【问题描述】:

模型.py

class Equipo(models.Model):
    CODIGO = models.CharField(primary_key=True, max_length=5 )
    DESCRIPCION = models.CharField(max_length=50, default='')
    TITULO = models.FileField(upload_to = "Archivos/Titulos/", default='', blank=True)

管理员.py

from django.contrib import admin
from .models import Equipo

class EquipoAdmin(admin.ModelAdmin):
    list_display = ('CODIGO', 'DESCRIPCION', 'TITULO')
admin.site.register(Equipo, EquipoAdmin)

我需要看到这样的东西

【问题讨论】:

    标签: django django-admin


    【解决方案1】:

    您可以使用下面的代码result

    # admin.py
    list_display = ('title', 'description', 'embed_pdf', 'deadline')
    
    def embed_pdf(self, obj):
        try:
            html = f'<iframe src="{obj.add_file.url}" width="200px" height="200px" frameborder="2"></iframe>'
            formatted_html = format_html(html.format(url=obj.add_file.url))
            return formatted_html
        except Exception as e:
            pass
    
    embed_pdf.short_description = "File/Image"
    admin.site.register(Order, OrderAdmin)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-12-03
      • 2015-01-10
      • 1970-01-01
      • 2011-04-29
      • 2012-10-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多