2

我不知道为什么 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'
)

非常感谢任何帮助!

4

2 回答 2

2

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

于 2013-08-05T10:30:15.480 回答
1

我有同样的问题。你可以试试这个:

  • 在您的 urls.py 中,将调用 include(admin.site.urls) 替换为: admin.site.urls

  • 在您的 setting.py 中,如果没有任何 TEMPLATE_CONTEXT_PROCESSORS 属性(这是我的情况),请添加:

    TEMPLATE_CONTEXT_PROCESSORS = ("django.contrib.auth.context_processors.auth"、"django.core.context_processors.debug"、"django.core.context_processors.i18n"、"django.core.context_processors.media"、"django.core. context_processors.static”、“django.core.context_processors.tz”、“django.contrib.messages.context_processors.messages”)

它似乎是普通 django 1.4 配置中的默认属性。以下是谈论它的文档:djangoproject-doc1 djangoproject-doc2

您可能还必须取消注释字符串:

# 'django.contrib.messages',
# 'django.contrib.staticfiles',

在 settings.py 的 INSTALLED_APPS 属性中,但我不确定。

很抱歉没有更好地解释这些变化的原因,但我是 django 初学者。我刚刚发现您的问题与我的问题相对应,然后是一个可能的遮阳篷。

我希望它可以帮助你。

编辑:如评论中所见,您可能会尝试删除有关管理站点 url 的行上的 url(...) 指令

于 2012-09-20T12:32:41.143 回答