Linking to Django Admin from 1.3

20 views
Skip to first unread message

eric.frederich

unread,
Jul 14, 2011, 10:35:22 AM7/14/11
to Django users
Hello,

I am moving from 1.0.2 to 1.3 and am having problems linking to the
admin site from within a template.
I used to have this in my template...

<p><a href="{% url the-django-admin None %}">Django Admin</a>

And I used to have this in my urls.py

url(r'^admin/(.*)' , admin.site.root, name='the-django-
admin'),

I'm sure this was hacky even back in 1.0.2
How should I link to the admin site's index from within a template?

I cannot hard code it to /admin because while this would work on
development, on production it needs to be /apps/admin
So I need to use the URL resoving mechanism so that the prefix gets
picked up based on which environment it is on.

So... simple question. How do I link to the Django admin site from
within a template?

Thanks,
~Eric

Alasdair Nicol

unread,
Jul 14, 2011, 10:52:34 AM7/14/11
to django...@googlegroups.com
From the django docs on url namespaces [1]

Namespaced URLs are specified using the : operator. For example, the main index page of the admin application is referenced using admin:index. This indicates a namespace of admin, and a named URL of index.

So you link to the admin index in your template using:

{% url admin:index %}

For Django 1.3, the documented way [2] to include the admin site is:
url(r'^admin/', include(admin.site.urls)),

Regards,
Alasdair Nicol
[1] https://docs.djangoproject.com/en/dev/topics/http/urls/#url-namespaces
[2] https://docs.djangoproject.com/en/1.3/intro/tutorial02/

-- 
Alasdair Nicol
Developer, MEMSET

mail: alas...@memset.com
 web: http://www.memset.com/

Memset Ltd., registration number 4504980. 25 Frederick Sanger Road, Guildford, Surrey, GU2 7YD, UK.

rebus_

unread,
Jul 14, 2011, 10:53:35 AM7/14/11
to django...@googlegroups.com
> --
> You received this message because you are subscribed to the Google Groups "Django users" group.
> To post to this group, send email to django...@googlegroups.com.
> To unsubscribe from this group, send email to django-users...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
>

Try:

{% url admin:index %}

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

eric.frederich

unread,
Jul 14, 2011, 10:59:26 AM7/14/11
to django...@googlegroups.com
Well... two people replied with the same answer so rather than picking favorites I'll just reply to myself.

This seems to work but looks a bit quirky.

When I use {% url admin:index %} the link that gets generated actually has "admin/(.*)" in it.
This even shows up in the url when I click on it.

Is this okay... is it a bug?

eric.frederich

unread,
Jul 14, 2011, 11:02:37 AM7/14/11
to django...@googlegroups.com
Nevermind... this was my bad.  I removed (.*) from my urls.py
This is now working... thanks a bunch.

This is what I get for developing an app that works and then not touching it for over a year.
I really need to keep up with these things.
Reply all
Reply to author
Forward
0 new messages