【问题标题】:Django 1.4.1 not loading admin site (Django Book tutorial)Django 1.4.1 未加载管理站点(Django Book 教程)
【发布时间】:2012-09-06 01:41:14
【问题描述】:

我不明白为什么 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))
)

这是一个 sn-p os 我的 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'
)

非常感谢任何帮助!

【问题讨论】:

  • 您请求的网址是“127.0.0.1:8000/admin”,您是否尝试使用尾部斜杠“127.0.0.1:8000/admin”?
  • 嗨,monkut - 刚刚尝试过,结果相同(没有结果)。我认为 Django 甚至没有查看我正在编辑的 urls.py 和/或 settings.py 文件 - 有没有办法找出它正在寻找的位置,或者指示它查看特定文件,这可以使用 pythonpath 完成?
  • 这不起作用,否则 /admin 会出现在错误消息中
  • 能否去掉url关键字,试试看?
  • 也许也删除任何 urls.pyc 文件。

标签: python django django-admin


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    我遇到了同样的问题。你可以试试这个:

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

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

      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-doc1djangoproject-doc2

    您可能还需要取消注释字符串:

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

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

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

    希望对你有帮助。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-01-10
      • 2011-09-23
      • 1970-01-01
      • 2015-01-22
      • 1970-01-01
      • 1970-01-01
      • 2012-09-11
      相关资源
      最近更新 更多