Getting Django admin url for an object

hasen picture hasen · Mar 29, 2009 · Viewed 145.9k times · Source

Before Django 1.0 there was an easy way to get the admin url of an object, and I had written a small filter that I'd use like this: <a href="{{ object|admin_url }}" .... > ... </a>

Basically I was using the url reverse function with the view name being 'django.contrib.admin.views.main.change_stage'

reverse( 'django.contrib.admin.views.main.change_stage', args=[app_label, model_name, object_id] )

to get the url.

As you might have guessed, I'm trying to update to the latest version of Django, and this is one of the obstacles I came across, that method for getting the admin url doesn't work anymore.

How can I do this in django 1.0? (or 1.1 for that matter, as I'm trying to update to the latest version in the svn).

Answer

markmuetz picture markmuetz · May 28, 2010

You can use the URL resolver directly in a template, there's no need to write your own filter. E.g.

{% url 'admin:index' %}

{% url 'admin:polls_choice_add' %}

{% url 'admin:polls_choice_change' choice.id %}

{% url 'admin:polls_choice_changelist' %}

Ref: Documentation