给你的django后台模型实例加上一个可以直接访问的链接

直接上代码:

admin.py

from django.core.urlresolvers import reverse
from django.utils.html import format_html

from .models import Flavor,IceCreamBar

class IceCreamBarAdmin(admin.ModelAdmin):
list_display = (“title”,”shell”,”filling”)
readonly_fields = (‘show_url’,)

def show_url(self,instance):
    url = reverse('flavors:ice_cream_bar_detail',kwargs={"pk":instance.pk})
    response = format_html("""<a href='{0}'>{1}</a>""",url,url)
    return response

show_url.short_description = "Ice Cream Bar URL"
show_url.allow_tags = True #永远不要将allow_tags暴露给未授权的用户!

admin.site.register(Flavor)
admin.site.register(IceCreamBar,IceCreamBarAdmin)

    原文作者:walterrwu
    原文地址: https://segmentfault.com/a/1190000000756488
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞