给你的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("""{1}""",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)

你可能感兴趣的:(django)