Skip to main content
 首页 » 编程设计

python之Django 1.4.1 不加载管理站点(Django Book 教程)

2024年08月30日28zfyouxi

我不明白为什么 Django 没有加载管理页面。似乎它甚至没有读取我正在编辑的 urls.py 文件——因为即使我注释掉“urlpatterns”语句,一旦我运行服务器,它仍然可以正常加载本地 hello 页面。

这是错误信息:

Page not found (404) 
Request Method: GET 
Request URL:    http://127.0.0.1:8000/admin 
Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order: 
^hello/$ 
^time/$ 
^time/plus/(\d{1,2})/$ 
The current URL, admin, didn't match any of these. 

这是我的 urlpatterns 代码:

from django.conf.urls import patterns, include, url 
from mysite.views import * 
 
# Uncomment the next two lines to enable the admin: 
from django.contrib import admin 
admin.autodiscover() 
 
urlpatterns = patterns('', 
    ('^hello/$', hello, ), 
    ('^time/$', current_datetime, ), 
    (r'^time/plus/(\d{1,2})/$', hours_ahead, ), 
    # Examples: 
    # url(r'^$', 'mysite.views.home', name='home'), 
    # url(r'^mysite/', include('mysite.foo.urls')), 
 
    # Uncomment the admin/doc line below to enable admin documentation: 
    # url(r'^admin/doc/', include('django.contrib.admindocs.urls')), 
 
    # Uncomment the next line to enable the admin: 
    url(r'^admin/', include(admin.site.urls)) 
) 

这是我的 settings.py 文件的一个片段:

 MIDDLEWARE_CLASSES = ( 
    'django.middleware.common.CommonMiddleware', 
    'django.contrib.sessions.middleware.SessionMiddleware', 
    # 'django.middleware.csrf.CsrfViewMiddleware', 
    'django.contrib.auth.middleware.AuthenticationMiddleware', 
    # 'django.contrib.messages.middleware.MessageMiddleware' 
    # Uncomment the next line for simple clickjacking protection: 
    # 'django.middleware.clickjacking.XFrameOptionsMiddleware', 
) 
 
ROOT_URLCONF = 'mysite.urls' 
 
# Python dotted path to the WSGI application used by Django's runserver. 
WSGI_APPLICATION = 'mysite.wsgi.application' 
 
TEMPLATE_DIRS = ( 
    '/Users/pavelfage/Desktop/Coding/mysite/Templates', 
    # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates". 
    # Always use forward slashes, even on Windows. 
    # Don't forget to use absolute paths, not relative paths. 
) 
 
INSTALLED_APPS = ( 
    'django.contrib.auth', 
    'django.contrib.contenttypes', 
    'django.contrib.sessions', 
    'django.contrib.sites', 
    # 'django.contrib.messages', 
    # 'django.contrib.staticfiles', 
    # Uncomment the next line to enable the admin: 
    'django.contrib.admin', 
    # Uncomment the next line to enable admin documentation: 
    # 'django.contrib.admindocs', 
    'mysite.books' 
) 

非常感谢任何帮助!

请您参考如下方法:

我遇到了同样的问题。在 urls.py 中取消注释 from django.contrib import admin 解决了这个问题。