Django中间件是不正确的配置?

时间:2012-11-08 02:01:29

标签: python django django-middleware

出于某种原因,我因自定义中间件而收到了一个ErrorperlyConfigured错误。

[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()

不幸的是,在这一点上我不太确定该尝试什么。这个错误非常无益。

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

任何想法可能是什么问题以及值得尝试的是什么?

3 个答案:

答案 0 :(得分: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”。

<强> EDITED

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

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

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

答案 1 :(得分:1)

是否存在以下文件?

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

如果没有,则找不到api/apiconsumer/authenticate.py

另外,请尝试从apiMIDDLEWARE_CLASSES中的前缀中删除INSTALLED_APPS。所以,你会有:

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

INSTALLED_APPS = (
    ...
    'courses',
    'apiconsumer',
    'static_content',
    ...
)

答案 2 :(得分:0)

init .py。

中使用导入时,我的配置不正确

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

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