我已经阅读了有关此问题的所有问题,但不幸的是,没有一个问题对我有用。
我的目标是在Django Admin ModelAdmin
中将自定义列显示为HTTP链接(<a>
标签)。
admin.py
def device_url(dev_id, dev_name):
html = '/v1/admin/devices/device/{}/change/'.format(dev_id)
return format_html('<a href="{0}">{1}</a>', html, dev_name)
@admin.register(Machine)
class MachineAdmin(admin.ModelAdmin):
form = MachineForm
list_display = ('name', 'location', 'devices', 'last_maintenance_log')
inlines = [CommentInline, ]
def devices(self, obj):
devices_with_links = ', '.join([device_url(d.id, d.name) for d in obj.devices.all()])
if len(devices_with_links) > 1:
return devices_with_links
else:
return '-'
devices.allow_tags = True
但是它仍在转义这些内容并将其显示为纯文本。
Devices
是device_set
模型的Machine
。 1机器:N个设备关系。
根据我在这里阅读的内容,即使在使用allow_tags=True
时没有format_html
,它也应该可以正常工作。
这是行不通的,因为内部函数devices
中没有format_html吗?如果没有,有人知道如何解决吗?