我遇到中间件问题我发现了很多关于它的问题,但在我的情况下没有任何帮助。
我使用中间件来获取current_user以在我的模型中使用以在save方法中保存修改后的用户而不在视图中写入此内容。
以下代码为original post:
中间件
from threading import local
_user = local()
class CurrentUserMiddleware(object):
def process_request(self, request):
_user.value = request.user
def get_current_user():
return _user.value
此代码中存在错误,因为我收到的错误如下:
Traceback (most recent call last):
File "C:\Program Files (x86)\Python35-32\Lib\wsgiref\handlers.py", line 137, in run
self.result = application(self.environ, self.start_response)
File "C:\Users\loc\dJangoEnvironment\lib\site-packages\django\contrib\staticfiles\handlers.py", line 63, in __call__
return self.application(environ, start_response)
File "C:\Users\loc\dJangoEnvironment\lib\site-packages\django\core\handlers\wsgi.py", line 158, in __call__
self.load_middleware()
File "C:\Users\loc\dJangoEnvironment\lib\site-packages\django\core\handlers\base.py", line 53, in load_middleware
mw_instance = mw_class()
TypeError: 'module' object is not callable
[20/Jul/2016 10:51:44] "GET /panel/ HTTP/1.1" 500 59
Traceback (most recent call last):
File "C:\Program Files (x86)\Python35-32\Lib\wsgiref\handlers.py", line 137, in run
self.result = application(self.environ, self.start_response)
File "C:\Users\loc\dJangoEnvironment\lib\site-packages\django\contrib\staticfiles\handlers.py", line 63, in __call__
return self.application(environ, start_response)
File "C:\Users\loc\dJangoEnvironment\lib\site-packages\django\core\handlers\wsgi.py", line 158, in __call__
self.load_middleware()
File "C:\Users\loc\dJangoEnvironment\lib\site-packages\django\core\handlers\base.py", line 53, in load_middleware
mw_instance = mw_class()
TypeError: 'module' object is not callable
[20/Jul/2016 10:51:44] "GET /favicon.ico HTTP/1.1" 500 59
我的中间件设置:
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.locale.LocaleMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.middleware.gzip.GZipMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.security.SecurityMiddleware',
'reversion.middleware.RevisionMiddleware',
'task.current_user',
)
你可以给我一些建议,哪里有错误或什么?我没有任何其他想法尝试,所以我希望你有一些。
答案 0 :(得分:6)
您必须提供中间件类的完整路径。不是包含中间件的模块。
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.locale.LocaleMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.middleware.gzip.GZipMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.security.SecurityMiddleware',
'reversion.middleware.RevisionMiddleware',
'task.current_user.CurrentUserMiddleware',
)
请注意,上面有很多例子。