1、安装redis库
pip install django-redis
2、配置django中的settings.py
# redis配置
CACHES = {
"default": {
"BACKEND": "django_redis.cache.RedisCache",
"LOCATION": "redis://127.0.0.1:6379", # 安装redis的主机的 IP 和 端口
"OPTIONS": {
"CLIENT_CLASS": "django_redis.client.DefaultClient",
"CONNECTION_POOL_KWARGS": {
"max_connections": 1000,
"encoding": 'utf-8'
},
"PASSWORD": "123" # redis密码
}
},
"master": {
"BACKEND": "django_redis.cache.RedisCache",
"LOCATION": "redis://127.0.0.2:6379", # 安装redis的主机的 IP 和 端口
"OPTIONS": {
"CLIENT_CLASS": "django_redis.client.DefaultClient",
"CONNECTION_POOL_KWARGS": {
"max_connections": 1000,
"encoding": 'utf-8'
},
"PASSWORD": "123123" # redis密码
}
}
}
3、调用redis
from django_redis import get_redis_connection
from django.shortcuts import HttpResponse
def index(request):
conn = get_redis_connection('default') # 连接redis的名称
return HttpResponse('连接成功')
标签:encoding,Redis,cache,redis,django,Django,连接
From: https://www.cnblogs.com/piggthird/p/17872977.html