django 管理员加载错误:/admin/login/ 处的 ImportError 没有名为后端的模块

Posted

技术标签:

【中文标题】django 管理员加载错误:/admin/login/ 处的 ImportError 没有名为后端的模块【英文标题】:django admin load error: ImportError at /admin/login/ No module named backends 【发布时间】:2015-10-06 15:00:34 【问题描述】:

突然之间,我似乎无法访问我的 django 应用程序的管理员。这是我收到的错误消息,知道会发生什么吗?自上次打开后,我认为我没有更改任何管理员或设置数据。

/admin/login/ 处的 ImportError 没有名为后端的模块 请求方法:POST 请求网址:http://localhost:8000/admin/login/?next=/admin/ Django 版本:1.9.dev20150119161257 异常类型:ImportError 异常值: 没有名为后端的模块 异常位置:/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/init.py in import_module,第 37 行 Python 可执行文件:/usr/bin/python Python版本:2.7.6 Python 路径: ['/Users/saraabi/Sites/django_testapp', '/Library/Python/2.7/site-packages/pip-6.0.6-py2.7.egg', '/用户/saraabi/django-trunk', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages', '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload', '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC', '/Library/Python/2.7/site-packages'] 服务器时间:2015年7月16日星期四23:31:24 +0000

环境:

请求方法:POST 请求 URL: http://localhost:8000/admin/login/?next=/admin/

Django 版本:1.9.dev20150119161257 Python 版本:2.7.6 已安装 应用程序:('django.contrib.admin','django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'testapp') 已安装的中间件: ('django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.auth.middleware.SessionAuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', 'django.middleware.security.SecurityMiddleware')

追溯:文件 “/Users/saraabi/django-trunk/django/core/handlers/base.py”在 获取响应 131. response = Wrapped_callback(request, *callback_args, **callback_kwargs) 文件 "/Users/saraabi/django-trunk/django/views/decorators/cache.py" 在 _wrapped_view_func 54. response = view_func(request, *args, **kwargs) 登录文件“/Users/saraabi/django-trunk/django/contrib/admin/sites.py” 388. return login(request, **defaults) 文件“/Users/saraabi/django-trunk/django/views/decorators/debug.py”中 sensitive_post_parameters_wrapper 76. 返回视图(request, *args, **kwargs) 文件 "/Users/saraabi/django-trunk/django/utils/decorators.py" in _wrapped_view 110. response = view_func(request, *args, **kwargs) 文件 "/Users/saraabi/django-trunk/django/views/decorators/cache.py" 在 _wrapped_view_func 54. response = view_func(request, *args, **kwargs) 登录时文件“/Users/saraabi/django-trunk/django/contrib/auth/views.py” 40. if form.is_valid(): 文件“/Users/saraabi/django-trunk/django/forms/forms.py” in is_valid 136. return self.is_bound 而不是 self.errors 文件“/Users/saraabi/django-trunk/django/forms/forms.py”出现错误 128. self.full_clean() 文件 "/Users/saraabi/django-trunk/django/forms/forms.py" 在 full_clean 345. self._clean_form() _clean_form 中的文件“/Users/saraabi/django-trunk/django/forms/forms.py” 369.cleaned_data = self.clean() 清理文件“/Users/saraabi/django-trunk/django/contrib/auth/forms.py” 165. password=password) 文件“/Users/saraabi/django-trunk/django/contrib/auth/init.py”中 认证 60.对于后端,backend_path in _get_backends(return_tuples=True): File "/Users/saraabi/django-trunk/django/contrib/auth/init.py" in _get_backends 27. backend = load_backend(backend_path) 文件“/Users/saraabi/django-trunk/django/contrib/auth/init.py”中 加载后端 21.返回import_string(path)()文件“/Users/saraabi/django-trunk/django/utils/module_loading.py”中 导入字符串 22. module = import_module(module_path) 文件“/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/init.py” 在 import_module 37. 进口(名称)

异常类型:/admin/login/ 处的 ImportError 异常值:否 名为后端的模块

【问题讨论】:

你能发布你的 settings.py 文件吗? 好的,刚刚贴在下面;谢谢! 【参考方案1】:
import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

DEBUG = True

TEMPLATE_DEBUG = True

ALLOWED_HOSTS = []

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'testapp',
)

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'django.middleware.security.SecurityMiddleware',
)

ROOT_URLCONF = 'django_testapp.urls'

TEMPLATES = [
    
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates')],
        'APP_DIRS': True,
        'OPTIONS': 
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        ,
    ,
]

WSGI_APPLICATION = 'django_testapp.wsgi.application'


DATABASES = 
    'default': 
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    


AUTHENTICATION_BACKENDS = [
    'testapp.backends.EmailBackend',
    'django.contrib.auth.backends.ModelBackend',
]

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


STATIC_URL = '/static/'

【讨论】:

【参考方案2】:

settings.py

import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
PROJECT_DIR = os.path.dirname(os.path.dirname(__file__) + '/../')

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.sites',
)

TEMPLATE_CONTEXT_PROCESSORS = (
    'django.contrib.auth.context_processors.auth',
    'django.core.context_processors.request',
)

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
)
SITE_ID = 1

ROOT_URLCONF = 'apps.urls'

WSGI_APPLICATION = 'apps.wsgi.application'

TEMPLATE_DIRS = (
    PROJECT_DIR + '/templates/',
)

DATABASES = 
    'default': 
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    

【讨论】:

以上是关于django 管理员加载错误:/admin/login/ 处的 ImportError 没有名为后端的模块的主要内容,如果未能解决你的问题,请参考以下文章

django创建项目问题解决办法

Django 1.9 加载数据错误

cPanel:Django Admin Portal 未加载静态/CSS 文件

为啥我的 Django 管理站点没有样式/CSS 加载?

Django Admin Kink 与折叠字段集

Django 静态文件未加载且没有错误迹象