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

相关推荐
天l志4 分钟前
Chrome Extension + 本地服务:浏览器页面上下文采集与远程执行技术设计
python·谷歌浏览器
CodexDave6 分钟前
Python 自动化接单实战(九):Windows 免环境交付如何打包与诊断
windows·python·自动化·python自动化·pyinstaller·软件交付·windows打包
CTA量化套保9 分钟前
2026年量化入门路线,概念规则和简单实现逐步走
人工智能·python
米码收割机13 分钟前
【Python】Python Django+Vue3校园自习室预约管理系统(源码+文档+PPT)【独一无二】
开发语言·python·django
AstartesEternal21 分钟前
python第二次作业(列表,字典)
开发语言·python
拉特莉的祈祷机23 分钟前
绿色测试日志为什么可能已经失效:用 AET 绑定命令与当前代码
python
三金1213838 分钟前
Python的一些内置模块
python
NPE~1 小时前
RAG 实战教程:从零构建企业智能问答 Agent
python·ai·实战·rag·agent搭建
BaoshengTT2 小时前
2026年双流区汽车贴膜种类及挑选要点保圣威固7V不凡门店
python
zhiSiBuYu05172 小时前
Flask 请求与响应新手实战指南
后端·python·flask