Django去访问web api接口Object of type Session is not JSON serializable

解决方案:settings.py中加入 :SESSION_SERIALIZER = 'django.contrib.sessions.serializers.PickleSerializer'

事由:Django去访问一个web api接口,两次连接之间需要通过Session()保持身份验证。

python 复制代码
        
def sendCode(request): 
    mobile =json.loads(request.body).get("Mobile")
    http = requests.Session()
    result = http.get(f'http://127.0.0.1:8000/api/login?mobile={mobile}&func=send_code')
    request.session['http'] = http
    request.session['httpMobile'] = mobile
    return result

def verifyCode(request): 
    code =json.loads(request.body).get("code")
    mobile = request.session.get('httpMobile')
    http = request.session.get('http')
    result = http.get(f'http://127.0.0.1:8000/api/login?mobile={mobile}&func=verify_code&code={code}')
    return result
复制代码
提示:Object of type Session is not JSON serializable

加入SESSION_SERIALIZER = 'django.contrib.sessions.serializers.PickleSerializer' 后解决。

相关推荐
小白学大数据19 小时前
浅析爬虫技术更迭:静态请求与浏览器渲染采集能力对比
爬虫·python·spring·数据分析
●VON19 小时前
鸿蒙Flutter实战:放弃sqflite选纯Dart JSON文件存储
flutter·华为·json·harmonyos·鸿蒙
亚林瓜子19 小时前
python的包管理器uv安装
python·uv·spec-kit
嗝o゚19 小时前
CANN pyasc 工具——Python 接口的算子开发
开发语言·python·cann·pyasc
Land032920 小时前
RPA替代方案:离线部署与Python扩展实战
开发语言·python·rpa
小糖学代码20 小时前
LLM系列:环境搭建:4.Nginx使用教程
运维·python·神经网络·nginx
muddjsv20 小时前
Python核心语法分类详解:从入门到精通
开发语言·windows·python
火星技术20 小时前
电影台词搜索引擎开源源码
搜索引擎·django·开源
高洁0121 小时前
智能体如何改变工作流一、工作流的“痛点
人工智能·python·数据挖掘·transformer·知识图谱