gpt4 book ai didi

django - ImproperlyConfigured middleware in Django project(导入redis功能报错)

转载 作者:可可西里 更新时间:2023-11-01 11:48:21 25 4
gpt4 key购买 nike

我正在为我的 Django 应用程序编写一个中间件,我从 request.session 获取一个变量,然后必须将它插入到 redis 排序集中。相当直接。

import os
#os.environ.setdefault("DJANGO_SETTINGS_MODULE","myproject.settings")
from myapp.redis_functions import set_whose_online

class WhoseOnlineMiddleware(object):
def process_request(self, request):
if '_auth_user_id' in request.session:
set_whose_online(request.session['_auth_user_id'])
else:
pass

redis_functions.py 包含我的应用程序中所有与 redis 相关的函数。 Redis 连接池也在那里实例化。

但这在 import 时给我一个 ImporperlyConfigured 错误。完整的回溯是:

Traceback (most recent call last):
File "/usr/lib/python2.7/wsgiref/handlers.py", line 85, in run
self.result = application(self.environ, self.start_response)
File "/home/myuser/.virtualenvs/myenv/local/lib/python2.7/site-packages/django/contrib/staticfiles/handlers.py", line 72, in __call__
return self.application(environ, start_response)
File "/home/myuser/.virtualenvs/myenv/local/lib/python2.7/site-packages/dj_static.py", line 83, in __call__
return self.application(environ, start_response)
File "/home/myuser/.virtualenvs/myenv/local/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 236, in __call__
self.load_middleware()
File "/home/myuser/.virtualenvs/myenv/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 53, in load_middleware
raise exceptions.ImproperlyConfigured('Error importing middleware %s: "%s"' % (mw_module, e))
ImproperlyConfigured: Error importing middleware myproject.middleware.WhoseOnline: "cannot import name add_filtered_post"

这很令人费解,因为我不是要导入 add_filtered_post,而是要导入 set_whose_onlineadd_filtered_postredis_functions.py 中的另一个函数,完全用于其他地方。

可能发生了什么,我该如何解决这个问题?我猜循环导入是罪魁祸首,但 python manage.py validate --traceback 返回 0 个错误。


这是我的 settings.py 中的内容:

MIDDLEWARE_CLASSES = (
'myproject.middleware.XForwardedFor.XForwardedForMiddleware',
'user_sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'myproject.middleware.WhoseOnline.WhoseOnlineMiddleware',
#'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'myproject.middleware.HellBanned.HellBannedMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'mobileesp.middleware.MobileDetectionMiddleware',
# Uncomment the next line for simple clickjacking protection:
'django.middleware.clickjacking.XFrameOptionsMiddleware',
)

注意:中间件添加到 myfolder/myproject/middleware/WhoseOnline.py,即与 settings.py、urls、templates 相同的文件夹

最佳答案

虽然发生这种情况可能有多种原因,但在我的特殊情况下,这是因为循环导入。我诊断并解决了问题,然后就消失了。

关于django - ImproperlyConfigured middleware in Django project(导入redis功能报错),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40864626/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com