0
class DirectAdmin(admin.ModelAdmin):
    def order_pdf(obj):
        # return "<a href='{}'>pdf</a>".format(
        url=reverse('orders:admin_order_pdf', args=[obj.id])
        return "http://localhost:8000" + url
    order_pdf.allow_tags = True
    order_pdf.short_description = 'PDF bill'
    list_display=['id','name','price','phone_number',order_pdf]
admin.site.register(Product)
admin.site.register(Category)
admin.site.register(Direct,DirectAdmin)

这是我的 admin.py。在我的对象的管理部分中,我想显示一个链接,该链接应充当锚点,它应重定向到下一个选项卡中的特定链接。

但是当我运行这段代码时,我可以看到 uri。

在此处输入图像描述

我想在我的 pdf 中将该部分作为锚点,重定向并在另一个选项卡中打开

那可能吗?

4

1 回答 1

0

您注释掉的代码几乎是正确的。您需要返回 html(例如<a href='{}'>pdf</a>),但您需要将输出标记为安全,以便它不会在模板中转义。

你可以用format_html这个。

class DirectAdmin(admin.ModelAdmin):
    def order_pdf(obj):
        url=reverse('orders:admin_order_pdf', args=[obj.id])
        return format_html("<a href='{}'>{}</a>", url, "pdf")
    order_pdf.allow_tags = True
    order_pdf.short_description = 'PDF bill'
    list_display=['id','name','price','phone_number',order_pdf]
于 2018-12-13T15:45:40.897 回答