For a django model, how can I get the django admin URL to add another, or list objects, etc.?

前端 未结 3 2162
攒了一身酷
攒了一身酷 2020-12-24 12:43

As much as I love the django documentation, the section on bookmarklets in the admin is strangely vague.

My question is this: If I\'m in a view and I have a django

3条回答
  •  天涯浪人
    2020-12-24 13:25

    You can retrieve this from the actual object instance, this worked for me:

    from django.core import urlresolvers
    from django.contrib.contenttypes.models import ContentType
    
    content_type = ContentType.objects.get_for_model(object.__class__)
    object_admin_url = django.core.urlresolvers.reverse("admin:%s_%s_change" % (content_type.app_label, content_type.model), args=(object.pk,))
    

    See this: http://djangosnippets.org/snippets/1916/

提交回复
热议问题