66

Very basic question, but I'm having trouble tracking down the answer on the web. I have a template, which I want to link to the django admin site (i.e. localhost:8000/admin). What is the code for this?

I'm imagining something like

<a href="{% url admin.site.root %}">link to admin panel</a>

However, when I try the above snippet I get:

Caught an exception while rendering:
  Reverse for 'project_name.django.contrib.admin.sites.root' with
  arguments '()' and keyword arguments '{}' not found.

Help?

ozan
  • 9,285
  • 4
  • 30
  • 27

2 Answers2

124

Try what Oggy is suggesting but then use ':' instead of '_' with the current Django:

<a href="{% url 'admin:index' %}">link to admin panel</a>
Maxime Lorant
  • 34,607
  • 19
  • 87
  • 97
Romain
  • 7,022
  • 3
  • 30
  • 30
  • This did the trick for me. With Django 1.3, using `admin_index` gave me a `TemplateSyntaxError`: `Caught NoReverseMatch while rendering: Reverse for 'admin_index' with arguments '()' and keyword arguments '{}' not found.` Using `admin:index` instead provided a working admin site URL. – Christopher Parker Jul 05 '11 at 16:57
  • 7
    For anyone coming here from Google, Django 1.5+ it's `{% url "admin:index" %}`, with the quotes – Eric Amorde Feb 28 '14 at 03:04
  • 2
    If you want to link to a subpage of the admin site, see [this answer](https://stackoverflow.com/a/2930241/7221965) – Alex Willison Aug 02 '17 at 18:24
  • What does 'admin:index' do and why do work other url paths in a different way (not with ":" )? – naheliegend Aug 25 '21 at 18:33
8

Which django version are you using? If you're using trunk, change your admin urlpatterns from:

(r'^admin/(.*)', admin.site.root)

to:

('^admin/', include(admin.site.urls))

And then you get a named URL pattern called 'admin_index' which you can refer to. See

http://docs.djangoproject.com/en/dev/ref/contrib/admin/#reversing-admin-urls

for more information

oggy
  • 1,855
  • 1
  • 13
  • 10