django admin显示所有应用程序、模型,但管理模型页面返回404
这些网址有效:
/admin
/admin/appname
但这不适用于任何模型,包括用户、组或站点:
/admin/appname/modelname
例如,/admin/auth
有效并显示组
和 Users
模型,但如果我单击任一模型并导航到 admin/auth/user
,我会收到 404 未找到错误。
我首先认为这可能是 urls.py 中的问题,所以我注释掉了除了管理员之外的所有内容。这没有什么区别。这是我的 urls.py 文件:
from django.conf.urls.defaults import *
from django.contrib import admin
urlpatterns = patterns('',
# main admin
(r'^admin/', include(admin.site.urls)),
)
admin.autodiscover()
这是我安装的应用程序,位于 settings.py 中:
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.admin',
'south',
)
我当然已经尝试关闭服务器,运行syncdb >,(以及为使用南的应用程序运行南迁移),并再次使用 runserver
。这没有什么区别。
我还尝试卸载并重新安装 django (相同版本,1.3)。
Django 按照以下顺序尝试了这些 URL 模式:
^admin/ ^$ [name='index']
^admin/^logout/$ [name='logout']
^admin/ ^password_change/$ [name='password_change']
^admin/ ^password_change/done/$ [name='password_change_done']
^admin/^jsi18n/$ [name='jsi18n']
^admin/ ^r/(?P
\d+)/(?P .+)/$ ^admin/ ^(?P
\w+)/$ [name='app_list'] 当前 URL
admin/auth/user/
与其中任何一个都不匹配。
These urls work:
/admin
/admin/appname
But this does not work for any model, including users, groups, or sites:
/admin/appname/modelname
For example, /admin/auth
works and shows the Groups
, and Users
models, but if I click on either model, navigating to admin/auth/user
, I receive a 404 not found error.
I first assumed that it was probably a problem in urls.py
so I commented out everything out besides the admin. It made no difference. Here's my urls.py
file:
from django.conf.urls.defaults import *
from django.contrib import admin
urlpatterns = patterns('',
# main admin
(r'^admin/', include(admin.site.urls)),
)
admin.autodiscover()
Here's my installed apps, in settings.py
:
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.admin',
'south',
)
I have of course already tried shutting down the server, running syncdb
, (as well as running south migrations for apps that were using south), and using runserver
again. It makes no difference.
I also tried uninstalling and reinstalling django (same version, 1.3).
Django tried these URL patterns, in this order:
^admin/ ^$ [name='index']
^admin/ ^logout/$ [name='logout']
^admin/ ^password_change/$ [name='password_change']
^admin/ ^password_change/done/$ [name='password_change_done']
^admin/ ^jsi18n/$ [name='jsi18n']
^admin/ ^r/(?P<content_type_id>\d+)/(?P<object_id>.+)/$
^admin/ ^(?P<app_label>\w+)/$ [name='app_list']
The current URL,
admin/auth/user/
, didn't match any of these.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如我在评论中提到的,您显然需要在将
admin.autodiscover()
行包含在urlpatterns
中的位置之前元组。这似乎是因为计算每个应用程序/模型的 url 的代码 -
admin.sites.AdminSite.get_urls
- 取决于在运行之前已为管理员注册的模型。如果不是,则不会创建相关 URL。As I mentioned in my comment, you apparently need to have the
admin.autodiscover()
line before the place where you include it in theurlpatterns
tuple.This seems to be because the code that calculates the urls for each app/model -
admin.sites.AdminSite.get_urls
- depends on the models already being registered for the admin before it runs. If not, the relevant URLs are not created.