Django 中间件:不是中间件模块错误
我正在使用 https://gist.github.com/426829 中提供的中间件来执行跨站点脚本编写。
但是,当我将中间件添加到 MIDDLEWARE_CLASSES
时,出现错误:
ImproperlyConfigured:不是中间件模块。
我的 MIDDLEWARE_CLASSES
看起来像这样:
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',
'TempMiddleware',)
我没有更改要点中的任何代码。其中有 process_request
和 process_response
方法。我在 Ubuntu 上运行最新版本的 Python 和 Django。
I am using the middleware provided in https://gist.github.com/426829 to do cross site scripting.
However, when I add the middleware to MIDDLEWARE_CLASSES
, I get the error:
ImproperlyConfigured: isn't a middleware module.
My MIDDLEWARE_CLASSES
looks like this:
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',
'TempMiddleware',)
I have not changed any code in the gist. process_request
and process_response
methods are there. I am on Ubuntu running the latest versions of Python and Django.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
什么是
TempMiddleware
?模块的名称,还是类的名称?正如您在其他条目中看到的那样,您需要实际类的完全限定的 Python 路径。如果TempMiddleware
是模块的名称,则需要TempMiddleware.MyMiddlewareClass
(并且您应该真正遵循 PEP8 命名约定) - 如果它是类的名称,则需要my_module.TempMiddleware
。What's
TempMiddleware
? The name of the module, or the name of the class? As you can see with the other entries, you need the fully-qualified Python path of the actual class. IfTempMiddleware
is the name of the module, you needTempMiddleware.MyMiddlewareClass
(and you should really follow PEP8 naming conventions) - and if it's the name of the class, you needmy_module.TempMiddleware
.编辑:
TempMiddleware
不可导入。这是类的名称,您必须输入整个导入路径。例如:
而不是
所以如果你的类位于 app_name/middleware.py 中,它应该是
它只是意味着在你的设置文件中,变量
MIDDLEWARE_CLASSES
包含一个模块列表,其中列出的模块之一不是中间件。可能的原因:
Edit:
TempMiddleware
is not importable. It's the name of the class, you must put the entire import path.E.g:
and not
So if your class is in app_name/middleware.py, it should be
It just mean that in your settings file, the variable
MIDDLEWARE_CLASSES
contains a list of modules in which one of the listed module is not a middleware.Possible causes: