Reputation: 1208
I've read every question here regarding this issue but unfortunately none of them is working for me.
My goal is to show custom column in Django Admin ModelAdmin
as HTTP Link (<a>
tag).
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
But it is still escaping those and showing it as plaintext.
Devices
is device_set
from Machine
model. 1 Machine : N Devices relationship.
According to what I've read here, it should just work, even without the allow_tags=True
when using format_html
.
Is this not working because I dont have format_html in the inner function devices
? If not, does anybody has an idea how to solve this?
Upvotes: 0
Views: 43
Reputation: 1425
In Django 2.0 the support for allow_tags in ModelAdmin methods was removed:
Support for the allow_tags attribute on ModelAdmin methods will be removed.
It seems you will instead want to use mark_safe:
return mark_safe(devices_with_links)
Upvotes: 1
Reputation: 1208
Nevermind I just solved this by changing
return devices_with_links
to
return mark_safe(devices_with_links)
Upvotes: 0