为什么Django无法与这个库通信?

1 投票
1 回答
1444 浏览
提问于 2025-04-16 09:36

我想使用Redis,所以我按照这个教程来操作: https://github.com/sebleier/django-redis-cache

首先,我安装了redis-py这个库: https://github.com/andymccurdy/redis-py/

接着,我在我的设置文件里加了这一行: CACHE_BACKEND = 'redis_cache.cache://localhost:6379'

然后我在views.py文件里做了这个操作:

from redis_cache import cache #this imports just fine!
cache.set("haha","lala")
print cache.get("haha")

但是出于某种原因,我遇到了一个AttributeError错误:

Exception Type: AttributeError at /
Exception Value: 'module' object has no attribute 'set'

1 个回答

3

你想要导入Django的缓存模块(这个模块其实是一个抽象层,最终会调用Redis),而不是直接使用Redis:

from django.core.cache import cache

撰写回答