博客
关于我
Django的session使用redis数据库作为缓存,控制台报错500,无具体错误提示
阅读量:671 次
发布时间:2019-03-15

本文共 1082 字,大约阅读时间需要 3 分钟。

一、redis作为Django session的缓存数据库设置步骤

(1)安装django-redis-sessions

pip install django-redis-sesisons

(2)修改settings.py

# session使用redis作为缓存,而不是内存SESSION_ENGINE = 'redis_sessions.session'SESSION_REDIS_HOST = "127.0.0.1"SESSION_REDIS_PORT = 6379SESSION_REDIS_DB = 0SESSION_REDIS_PASSWORD = 'face2020'SESSION_REDIS_PREFIX = 'session'

(3)url配置

from django.urls import path, re_pathfrom app1 import viewsurlpatterns = [     re_path(r'setSession/$', views.setSession),    re_path(r'getSession/$', views.getSession),]

(4)视图函数

def setSession(request):        # 存储session    request.session['name'] = name    request.session.set_expiry(300)    return HttpResponse('success')def getSession(request):    name = request.session['name']    return HttpResponse(name)

(5)redis查看

 

 (6)浏览器输入url报错

http://127.0.0.1:8000/setSession/控制台报错如下[10/Apr/2020 19:31:50] "GET/setSession/ HTTP/1.1" 500 145

二、解决方案

(1)修改安装的django-redis-sessions版本,不指定默认安装最新版本。当发现出现错误且不提示错误时说明是包内部错误,需要尝试更换包版本

pip install django-redis-sessions==0.5.6# 可查看包的所有版本pip install django-redis-sessions==#可进行包的卸载pip uninstall django-redis-sessions

 

转载地址:http://vnglz.baihongyu.com/

你可能感兴趣的文章
NN&DL4.8 What does this have to do with the brain?
查看>>
nnU-Net 终极指南
查看>>
No 'Access-Control-Allow-Origin' header is present on the requested resource.
查看>>
No 'Access-Control-Allow-Origin' header is present on the requested resource.
查看>>
NO 157 去掉禅道访问地址中的zentao
查看>>
no available service ‘default‘ found, please make sure registry config corre seata
查看>>
No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
查看>>
no connection could be made because the target machine actively refused it.问题解决
查看>>
No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
查看>>
No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
查看>>
No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
查看>>
No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
查看>>
No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
查看>>
No module named 'crispy_forms'等使用pycharm开发
查看>>
No module named 'pandads'
查看>>
No module named cv2
查看>>
No module named tensorboard.main在安装tensorboardX的时候遇到的问题
查看>>
No module named ‘MySQLdb‘错误解决No module named ‘MySQLdb‘错误解决
查看>>
No new migrations found. Your system is up-to-date.
查看>>
No qualifying bean of type XXX found for dependency XXX.
查看>>