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

相关推荐
用户03321266636712 分钟前
如何在 Python 环境中裁剪 PDF 页面
python
威联通网络存储15 分钟前
TS-h2287XU-RP 在医药制造批记录与合规归档场景的部署
python·制造
蓝鸟197422 分钟前
Python Flask + Oracle 接口开发 小白完整版笔记(入参/查库/批量/JSON/避坑)
python·oracle·flask
正在走向自律28 分钟前
从 Python 基础到大模型落地:读《深入浅出 Python 人工智能》,吃透 AI 工程化 CRUD 实战
开发语言·人工智能·python·机器学习·知识脉络
171320330计算机毕设编程30 分钟前
2027计算机毕设五大方向对比&选题推荐
java·ide·python·算法·django·php·推荐算法
Zane199432 分钟前
去重用 set 到底能快多少?实测差距接近三百倍
后端·python
岁月宁静34 分钟前
二、《从零手撸 Agent》 — 聊聊 LLM 的失忆真相
前端·python·agent
0xBADCODE41 分钟前
动态DEX加载+反射+DES硬编码密钥:安卓三层逆向实战
android·java·python·安全·网络安全·逆向·ctf
岁月宁静1 小时前
一、《从零手撸 Agent》 我用 10 行代码跑通了第一次大模型调用(顺便踩了 4 个坑)
前端·python·agent
大连好光景1 小时前
【AI Agent案例开发项目01解读】
chrome·python·langchain