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' 后解决。

相关推荐
千里码aicood几秒前
flask基于数字人的储粮知识问答原型系统研究与实现
后端·python·flask
E_ICEBLUE7 分钟前
Python 实现 Markdown 转 Word、PDF:从转换到页面设置
python·pdf·word·markdown·格式转换
哒咩哒咩12910 分钟前
Agent 智能体开发全攻略:从 ReAct 到企业级架构
python·langchain·fastapi
H愚公移山H18 分钟前
Tengine2.4.1 + OpenSSL1.1.1w 全平台编译踩坑手册(CentOS7 x86_64|Linux x64|ARM64|2026实战复盘)
python
码云骑士29 分钟前
120-视频理解-大模型视频分析-抽帧-自动字幕摘要-高光片段
python·音视频
xfan_me35 分钟前
手机在网状态接口-空号查询-空号过滤API
数据库·人工智能·python·智能手机
测试秃头怪38 分钟前
Postman中变量的使用
自动化测试·软件测试·python·测试工具·测试用例·接口测试·postman
ynchyong39 分钟前
词云(Word Cloud) 使用方法
python·统计·词云
Alkaid20771 小时前
我把文件和脚本放一起了,Python 就是看不见?新手必看的绝对路径 vs 相对路径终极避坑指南
python·ai编程
光泽雨1 小时前
JSON /XML转换
xml·json