Is there any possibility to get object delete url in Django Admin, in change list template (change_list_results.html)? I must add icons (edit/delete) on results list and try to get that links for object.
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
The delete url for a particular instance can be obtained via:
info = obj._meta.app_label, obj._meta.module_name
reverse('admin:%s_%s_delete' % info, args=(obj.id,))
So, just add a method to your ModelAdmin
to return that URL wrapped in appropriate HTML:
def delete_link(self, obj):
info = obj._meta.app_label, obj._meta.module_name
url = reverse('admin:%s_%s_delete' % info, args=(obj.id,))
return '<a href="%s">Delete</a>' % url
delete_link.allow_tags = True
delete_link.short_description = 'Delete'
And, then add that to list_display
, like any other field on your model.