Django管理页面:输入对象' CommonMiddleware'没有属性' is_usable'

时间:2013-04-07 10:34:20

标签: django django-admin

我正在研究Django的数据库密集型Web应用程序。我从遵循基本教程开始,然后添加了MySQL数据库并为模型,视图和测试创建了一个文件夹。 (我删除了旧的models.py,tests.py,views.py。)我可以创建新模型并将它们与我的数据库同步。

在我决定尝试使用Django管理界面之前,一切似乎都有效。我收到的错误并不能很好地解释自己:

AttributeError at /admin/
type object 'CommonMiddleware' has no attribute 'is_usable'

我不知道造成这种情况的原因。这是我的urls.py

urlpatterns = patterns(
'',
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,删除了评论:

DEBUG = True
TEMPLATE_DEBUG = DEBUG

ADMINS = (
    ('admin', 'admin@example.com'),
)

MANAGERS = ADMINS

DATABASES = dict(
    default=dict(
        ENGINE='django.db.backends.mysql',     
        NAME='mydbname',                   
        USER='root',
        PASSWORD='******',
        HOST='',        
        PORT='8000',    
    )
)

# This is redirected to localhost by C:Windows/System32/drivers/etc/hosts
ALLOWED_HOSTS = ['mysite.com']
TIME_ZONE = 'Europe/Amsterdam'
LANGUAGE_CODE = 'en-us'
SITE_ID = 1
USE_I18N = True
USE_L10N = True
USE_TZ = True
MEDIA_ROOT = ''
MEDIA_URL = ''
STATIC_ROOT = ''
STATIC_URL = '/static/'
STATICFILES_DIRS = (    )
STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)

SECRET_KEY = '****************************************'

TEMPLATE_LOADERS = (
    'django.template.loaders.filesystem.Loader',
    'django.template.loaders.app_directories.Loader',
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
)

ROOT_URLCONF = 'bandage.urls'

WSGI_APPLICATION = 'bandage.wsgi.application'

import os
TEMPLATE_DIRS = (os.path.join(os.path.dirname(__file__), '..', 'templates').replace('\\', '/'),)

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',
    'application',
)

正如您所看到的,我启用了管理站点并找到了URL,但是在Django库中的模块中出现了一些错误。以下是完整的跟踪:

Environment:


Request Method: GET
Request URL: http://mysite.com/admin/

Django Version: 1.5.1
Python Version: 2.7.0
Installed Applications:
('django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django.contrib.admin')
Installed Middleware:
('django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware')


Traceback:
File "D:\Python27\lib\site-packages\django-1.5.1-py2.7.egg\django\core\handlers\base.py" in get_response
  140.                     response = response.render()
File "D:\Python27\lib\site-packages\django-1.5.1-py2.7.egg\django\template\response.py" in render
  105.             self.content = self.rendered_content
File "D:\Python27\lib\site-packages\django-1.5.1-py2.7.egg\django\template\response.py" in rendered_content
  80.         template = self.resolve_template(self.template_name)
File "D:\Python27\lib\site-packages\django-1.5.1-py2.7.egg\django\template\response.py" in resolve_template
  58.             return loader.get_template(template)
File "D:\Python27\lib\site-packages\django-1.5.1-py2.7.egg\django\template\loader.py" in get_template
  146.     template, origin = find_template(template_name)
File "D:\Python27\lib\site-packages\django-1.5.1-py2.7.egg\django\template\loader.py" in find_template
  129.             loader = find_template_loader(loader_name)
File "D:\Python27\lib\site-packages\django-1.5.1-py2.7.egg\django\template\loader.py" in find_template_loader
  112.         if not func.is_usable:

Exception Type: AttributeError at /admin/
Exception Value: type object 'CommonMiddleware' has no attribute 'is_usable'

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

您的TEMPLATE_LOADERS中有一些中间件。删除它们。

TEMPLATE_LOADERS = (
    'django.template.loaders.filesystem.Loader',
    'django.template.loaders.app_directories.Loader',
)                              #\
                               # | it seems that at some point
                               # | you accidentally removed these
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',
)