Nick Heiner
Nick Heiner

Reputation: 122450

Django: Programmatically determine path to an admin page?

If I have the admin module enabled for a certain model, is there a way to determine what the full URL will be for the page to create a new instance of that model?

What I'm looking for is something like this following:

"Please <a href='%s'>create</a> some Foo models" % Foo.AdminCreateUrl

Update: To clarify, this needs to be done on the backend rather than on the templating layer.

This is what I'm currently doing:

DisabledMessage = "No foos exist. Please <a href='%s'>create</a> some before assigning bars." \
                    % urlresolvers.reverse('admin:app_foo_add')

This does not work. I get the following error message:

ImproperlyConfigured at /admin/bar/bar/add/

The included urlconf mysite.urls doesn't have any patterns in it

What am I doing wrong?

Upvotes: 1

Views: 210

Answers (2)

S.Lott
S.Lott

Reputation: 391852

First, read this. The admin site must be properly activated.

http://docs.djangoproject.com/en/1.2/ref/contrib/admin/#overview

Second, read this. This is how the admin URI names work.

http://docs.djangoproject.com/en/1.2/ref/contrib/admin/#admin-reverse-urls

I think you can use the {% url %} template tag.

Please <a href="{% url "admin:app_Foo_add" %}">create</a> some Foo models

Upvotes: 3

Daniel Roseman
Daniel Roseman

Reputation: 599610

Reversing admin urls.

Upvotes: 1

Related Questions