0

我正在研究 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'

任何帮助是极大的赞赏!

4

1 回答 1

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',
)
于 2013-04-07T10:35:38.803 回答