AttributeError: 'NoneType' object has no attribute 'find', cache error

AttributeError: 'NoneType' object has no attribute 'find', cache error

在我的本地环境中,django 开发服务器工作正常,没有错误。

当我在使用 nginx 和 gunicorn 的生产环境中 运行 应用程序时,出现此错误:

AttributeError: 'NoneType' object has no attribute 'find'

这是错误的来源:

if cache.get('ratings').find(name_input) == -1:
    result = food.objects.get(name = name_input)

我导入了内存缓存:

from django.core.cache import cache

在我的 settings.py:

CACHES = {
   'default': {
       'BACKEND':   
       'django.core.cache.backends.memcached.MemcachedCache',                                                                                                                                                                                                                                                   
        'LOCATION': '127.0.0.1:11211',
    }
}

我希望网页能够正常运行

--------编辑:-------------

提供更多信息 我的 HTML 部分有一个 JSON 脚本,可将变量发送到 python。 然后 python 抓住它。

我再说一遍,一切都像本地魅力一样。

===================================

--------编辑新的------ 我试图更改内存缓存的位置以指向我在 "settings.py":

中的服务器
CACHES = {
    'default': {
        'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
        'LOCATION': 'MY_IP:11211',
}

}

但是这次尝试没有成功 我可以正常打印到系统日志,值不是 NULL

我相信 cache.get('ratings') 无法找到字符串 'ratings' 所以它默认 returns NoneType,所以它无法使用 .find 属性。 您应该尝试打印缓存以查看其中是否有字符串评级

错误是我一开始没有安装memcached;即使我使用此命令安装它:

pip3 install python-memcached

然后我改用另一个命令:

sudo apt install memcached

现在一切正常:)