给你的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)
评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论