citmusa
11/9/2016 - 2:50 AM

Django admin URL template tag

Django admin URL template tag

# Usage: <a href="{% admin_url model_instance %}">admin edit page</a>

from django.core import urlresolvers
from django.contrib.contenttypes.models import ContentType

from django import template
register = template.Library()

@register.simple_tag
def admin_url(instance):
    try:
        content_type = ContentType.objects.get_for_model(instance.__class__)
        return urlresolvers.reverse("admin:%s_%s_change" % (content_type.app_label, content_type.model), args=(instance.id,))
    except:
        return ""