0

出于某种原因,由于自定义中间件,我收到了 ImproperlyConfigured 错误。

[Wed Nov 07 20:47:07 2012] [error] [client 158.130.107.158] File does not exist: /home/davidxu/public_html/favicon.ico
[Wed Nov 07 20:47:09 2012] [error] [client 158.130.107.158] mod_wsgi (pid=24114): Exception occurred processing WSGI script '/home/davidxu/dev/Penn-Course-Review-api/api/django.wsgi'.
[Wed Nov 07 20:47:09 2012] [error] [client 158.130.107.158] Traceback (most recent call last):
[Wed Nov 07 20:47:09 2012] [error] [client 158.130.107.158]   File "/usr/local/lib/python2.6/dist-packages/django/core/handlers/wsgi.py", line 219, in __call__
[Wed Nov 07 20:47:09 2012] [error] [client 158.130.107.158]     self.load_middleware()
[Wed Nov 07 20:47:09 2012] [error] [client 158.130.107.158]   File "/usr/local/lib/python2.6/dist-packages/django/core/handlers/base.py", line 47, in load_middleware
[Wed Nov 07 20:47:09 2012] [error] [client 158.130.107.158]     raise exceptions.ImproperlyConfigured('Error importing middleware %s: "%s"' % (mw_module, e))
[Wed Nov 07 20:47:09 2012] [error] [client 158.130.107.158] ImproperlyConfigured: Error importing middleware api.apiconsumer.authenticate: "No module named api.apiconsumer.authenticate"

作为参考,以下是文件的相关部分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',
    'django.middleware.cache.UpdateCacheMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.cache.FetchFromCacheMiddleware',
    'api.apiconsumer.authenticate.Authenticate',
)

INSTALLED_APPS = ( 
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'api.courses',
    'api.apiconsumer',
    # Uncomment the next line to enable the admin:
    'django.contrib.admin',
    'api.static_content',
    'django_extensions', # used for debugging, remove if problematic
    'django.contrib.staticfiles',
    # Uncomment the next line to enable admin documentation:
    # 'django.contrib.admindocs',
)

以及django.wsgi文件的价值。(注意,这里有很多自定义的东西。)

import os,sys

PROJECT_PATH = os.path.realpath(os.path.dirname(__file__))

sys.path.append(PROJECT_PATH)


from sandbox_config import *

#Uncomment these two lines to use virtualenv
#activate_this = os.path.join(COURSESAPI_APP_ROOT, "ENV/bin/activate_this.py")
#execfile(activate_this, dict(__file__=activate_this))

sys.path.append(DEV_ROOT)
sys.path.append(COURSESAPI_APP_ROOT)

os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'

import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

不幸的是,此时我不确定该尝试什么。该错误非常无用。

  • 删除有问题的行导致下一个自定义中间件破坏程序,这让我认为这可能是一个路径问题。
  • 谷歌似乎没有太多帮助。

有什么想法可能是什么问题,什么值得尝试?

4

3 回答 3

3

这个文件在哪里 ( api/apiconsumer/authenticate.py)

No module named api.apiconsumer.authenticate

位于(你能告诉我们你这个文件的绝对路径吗?)?

它实际上位于/home/davidxu/dev/Penn-Course-Review-api/api/apiconsumer/authenticate.py

如果它不在与您的 wsgi 进程的 PYTHONPATH 相关的正确文件系统路径中,它将无法加载该模块中的代码。

此外,您的“api”应用程序是否列在您的 settings.py 中INSTALLED_APPS?如果您没有将“api”列为INSTALLED_APPS应用程序,Django 将不知道搜索“api.apiconsumer.authenticate”。

已编辑

OP 告诉我这api实际上是他的项目的名称,他的应用程序名称是apiconsumer.

所以我的回答解决了这个问题: -

我建议您将 django 项目名称与应用程序名称明确分开。因此apiconsumer,在您的 INSTALLED_APPS 中用作应用程序名称并apiconsumer.authenticate.Authenticate在您的 MIDDLEWARE_CLASSES 中使用。

于 2012-11-08T02:05:07.490 回答
1

Do the following files exist?

api/__init__.py
api/apiconsumer/__init__.py

If not, then api/apiconsumer/authenticate.py won't be found.

Also, try removing api from the prefix in the MIDDLEWARE_CLASSES and INSTALLED_APPS. So, you would have:

MIDDLEWARE_CLASSES = (
    ...
    'apiconsumer.authenticate.Authenticate',
)

INSTALLED_APPS = (
    ...
    'courses',
    'apiconsumer',
    'static_content',
    ...
)
于 2012-11-08T02:16:46.573 回答
0

在init .py中使用导入时配置不正确。

症状是: - runserver 就像一个魅力 - 部署时 (apache/nginx+uwsgi) 导入模块时出现许多错误,最后引发了 ImproperlyConfigured

解决方案:将所有init .py 文件留空。

于 2013-08-17T17:24:38.910 回答