python - 为什么我不能让 Django 与这个库对话?

标签 python database django caching redis

我想使用 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'

最佳答案

你想导入Django的缓存模块(最终调用Redis的抽象层)而不是直接使用Redis:

from django.core.cache import cache

关于python - 为什么我不能让 Django 与这个库对话?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4631719/

相关文章:

python - 如何使用 Python tkinter 在另一个框架的文本框中显示过程输出

java - 多态性和树结构

html - Postgresql Ltree 查询获取嵌套 json/array 的注释线程并从中构建 HTML

django - 字段不显示在 django 管理中

python - Django:进行数据库查询时出现UnboundLocalError

python - Spark 无法 pickle method_descriptor

python - django.db.migrations 是否有 `update migration`(ala South)选项?

python - Spacy (Python 3.10) token.lefts 方法错误地返回空列表

Java Combobox,管理数据库中的 2 个字段

django - 无法使用 curl 通过 Django OAuth Toolkit 获取 token