views:

927

answers:

3

In the The Django Book in chapter 6 about the Admin Site, they tell me to add the follwing URLpattern to urls.py:

urlpatterns = patterns('',
    # ...
    (r'^admin/', include(admin.site.urls)),
    # ...
)

But to make it work on my system, I had to uncomment the following line:

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

Can somebody enlighten me on what the differences are?

A: 

The Django Book speaks of version 0.9.6. Since then the admin has been rewritten. In Django 1.0 the whole admin is served by a single view (admin.site.root) which parses the rest of the URL internally.

Compare the admin directory of 0.96.3 with the corresponding directory from 1.0.2. There is no urls.py in the latter.

Antti Rasinen
A: 

from the source code for the admin.site.root function:

root(self, request, url): Handles main URL routing for the admin app.

[...] method can be used as a Django view function that presents a full admin interface for the collection of registered models.

Gabriel Hurley
+4  A: 

Both Gabriel and Antti have it the wrong way round, unfortunately.

admin.site.root is the version 1.0 behaviour. If you have downloaded 1.0 or 1.0.2, that's what you should use.

However, there were some changes to the URL handling for Django's admin very recently, which are part of the yet-to-be-released 1.1. These are primarily to make it possible to use the reverse() function to look up admin URLs. So if you have a recent checkout of the code, you'll need to use admin.site.urls.

Your link is to the second edition of the Django Book, which is being updated for version 1.1 - and the docs which Gabriel refers to are also for the current checkout, which has the new version.

(Just for completeness, I'd note that versions of Django before newforms-admin was merged, prior to 1.0, used admin.urls, not admin.site.urls or admin.site.root.)

Daniel Roseman