在Django admin.py中,如何在管理视图中添加自定义链接/ URL字段download_pdf?
class CustomerAdmin(admin.ModelAdmin):
list_display = ['first_name', 'last_name', 'full_name', 'download_pdf']
def full_name(self, obj):
return obj.first_name + " " + obj.last_name
def download_pdf(self, obj):
return "<a href=''>PDF</a>" # this is not working
上面的代码不起作用,download_pdf字段显示为文本,但未显示链接。
使用format_html(...)
-(Django doc)函数
from django.utils.html import format_html
class CustomerAdmin(admin.ModelAdmin):
list_display = ['first_name', 'last_name', 'full_name', 'download_pdf']
def full_name(self, obj):
return obj.first_name + " " + obj.last_name
def download_pdf(self, obj):
return format_html("<a href=''>PDF")
例:
def download_pdf(self, obj):
return format_html(f"<a href='/foo/bar/{obj.pk}/download-pdf/'>PDF</a>")
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句