admin.site.root和admin.site.urls之间的区别

时间:2022-07-25 15:05:25

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

在第6章关于管理站点的The Django Book中,他们告诉我将以下URLpattern添加到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?

有人可以告诉我有什么不同之处吗?

4 个解决方案

#1


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

不幸的是,Gabriel和Antti都错了。

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.

admin.site.root是版本1.0的行为。如果您已下载1.0或1.0.2,那就是您应该使用的。

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.

但是,最近Django管理员的URL处理发生了一些变化,这些变化是尚未发布的1.1的一部分。这些主要是为了使用reverse()函数来查找管理URL。因此,如果您最近检查了代码,则需要使用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.

您的链接是Django Book的第二版,该版本正在针对1.1版进行更新 - 而Gabriel所引用的文档也适用于当前的结帐,其中包含新版本。

(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.)

(为了完整起见,我注意到在newforms-admin合并之前的Django版本,在1.0之前,使用了admin.urls,而不是admin.site.urls或admin.site.root。)

#2


Please notice the following; I struggled because of (.*) being in the second entry below.

请注意以下事项;我因为(。*)在下面的第二个条目中而挣扎。

Works, but is deprecated:

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

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

Incorrect, and partially works:

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

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

Correct, and works well:

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

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

#3


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.

Django Book讲的是版本0.9.6。从那以后,管理员已被重写。在Django 1.0中,整个管理员由一个视图(admin.site.root)提供服务,该视图在内部解析URL的其余部分。

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

将0.96.3的admin目录与1.0.2中的相应目录进行比较。后者没有urls.py.

#4


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

从admin.site.root函数的源代码:

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

root(self,request,url):处理管理应用程序的主URL路由。

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

[...]方法可以用作Django视图函数,为注册模型的集合提供完整的管理界面。

#1


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

不幸的是,Gabriel和Antti都错了。

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.

admin.site.root是版本1.0的行为。如果您已下载1.0或1.0.2,那就是您应该使用的。

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.

但是,最近Django管理员的URL处理发生了一些变化,这些变化是尚未发布的1.1的一部分。这些主要是为了使用reverse()函数来查找管理URL。因此,如果您最近检查了代码,则需要使用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.

您的链接是Django Book的第二版,该版本正在针对1.1版进行更新 - 而Gabriel所引用的文档也适用于当前的结帐,其中包含新版本。

(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.)

(为了完整起见,我注意到在newforms-admin合并之前的Django版本,在1.0之前,使用了admin.urls,而不是admin.site.urls或admin.site.root。)

#2


Please notice the following; I struggled because of (.*) being in the second entry below.

请注意以下事项;我因为(。*)在下面的第二个条目中而挣扎。

Works, but is deprecated:

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

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

Incorrect, and partially works:

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

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

Correct, and works well:

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

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

#3


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.

Django Book讲的是版本0.9.6。从那以后,管理员已被重写。在Django 1.0中,整个管理员由一个视图(admin.site.root)提供服务,该视图在内部解析URL的其余部分。

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

将0.96.3的admin目录与1.0.2中的相应目录进行比较。后者没有urls.py.

#4


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

从admin.site.root函数的源代码:

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

root(self,request,url):处理管理应用程序的主URL路由。

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

[...]方法可以用作Django视图函数,为注册模型的集合提供完整的管理界面。