Skip to content

Instantly share code, notes, and snippets.

@citmusa
Created November 9, 2016 02:50
Show Gist options
  • Save citmusa/fd09fcece582b6a7b19fce1cc35ffc20 to your computer and use it in GitHub Desktop.
Save citmusa/fd09fcece582b6a7b19fce1cc35ffc20 to your computer and use it in GitHub Desktop.
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 ""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment