1

直接上代码:

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
125 声望2 粉丝