1、在Django项目的settings文件里定义里变量
REDIS_CONN = {
'HOST':'127.0.0.1',
'PORT':6379,
'DB':0,
}
2、函数需要使用settings里定义的变量需要在你的文件里添加
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "你的project.settings")
如下:
#_*_coding:utf-8_*_
import redis
import io
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "CrazyMonitor.settings")
def redis_conn(django_settings):
#print(django_settings.REDIS_CONN)
pool = redis.ConnectionPool(host=django_settings.REDIS_CONN['HOST'],
port=django_settings.REDIS_CONN['PORT'],
db=django_settings.REDIS_CONN['DB'])
r = redis.Redis(connection_pool=pool)
print(r)
return r
from django.conf import settings
redis_conn(settings)