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

相关推荐
梯度下降者27 分钟前
零基础入门Python 爬虫
爬虫·python·数据分析·入门指南·webcrawler
光影6271 小时前
Unittest单元测试基础速记
测试工具·单元测试·json·测试覆盖率
“AI国潮设计-小江”2 小时前
【AIGC实战】Python+Stable Diffusion打造“英歌舞财神”IP,解锁“马到成功”等爆款情绪营销文案设计
开发语言·人工智能·python·prompt·aigc
ZLongTeng3 小时前
EIDE的launch.json文件
json
benchmark_cc3 小时前
用 Python 拉 A 股全市场实时快照:Notebook 请求、结果校验与易错点
python·数据分析·pandas·量化交易·股票数据·quantdash
伞伞悦读3 小时前
【第70期】Adam二阶矩不是方差:偏差校正、动量与AdamW完整实验
python
外收内放4 小时前
Python基础语法练习题(73)
java·前端·python
锋行天下4 小时前
Langgraph HITL 中断运行,用户输入后输出打字效果
人工智能·python
时早玖沐4 小时前
给 AstrBot 写一个天气推送插件:12 次提交、6 个坑
python·开源·ai编程
触底反弹4 小时前
🚀 FastAPI 五大重难点深度剖析:从 500 报错到彻底搞懂 Depends、Pydantic 与 SQLAlchemy
python·fastapi