如何在django admin中创建自定义django admin url的链接?

时间:2017-07-18 15:08:49

标签: python django django-admin

如何在django admin中创建链接到自定义django admin url我在django admin中有2个应用程序,我想从应用程序2中的app1链接到自定义URL

admin.py APP1

class APP1Admin(ModelAdmin):
    list_display = ('xx','request_me')

    def request_me(self,obj):

       reverse_path = reverse("admin: APP2_TargetLink",args=(obj.pk,)) # My Problem is  How to link to func APP2 target link

       return '<a href="%s"> link </a>'%(reverse_path)

    request_me.allow_tags =True

admin.py APP2

class APP2Admin(ModelAdmin):

   def get_urls(self):
       urls = super(APP2Admin, self).get_urls()
       my_urls = [
       url(r'(\d*)/target_link/$', self.admin_site.admin_view(self.target_link_view),name="TargetLink"),
    ]
      return my_urls + urls

    def target_link_view(self,request,id):
       ...
       return TemplateResponse(request, template, context)

1 个答案:

答案 0 :(得分:1)

定义网址格式时,您有name="TargetLink",因此可以使用

反转网址
reverse("admin:TargetLink",args=(obj.pk,))

如果您希望app2位于网址格式名称中,则必须自行添加,例如:

   url(r'(\d*)/target_link/$', self.admin_site.admin_view(self.target_link_view),name="app2_TargetLink"),

然后用:

反转它
reverse("admin:app2_TargetLink",args=(obj.pk,))