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

相关推荐
zh路西法19 分钟前
【现代控制理论与卡尔曼滤波】从状态空间到Python仿真实现
开发语言·python
Vodka~1 小时前
WSL2 + RViz GPU渲染机械臂
人工智能·python
8Qi81 小时前
hello-agents学习笔记--Memory让Agent拥有记忆
人工智能·python·llm·agent·ai编程·vibecoding
Esaka_Forever1 小时前
Python 完整内存管理机制详解
开发语言·python·spring
PixelBai2 小时前
JSON转表格使用教程:从入门到精通
json
Weigang2 小时前
用 LlamaIndex 做 RAG 前,先把 Reader、Index、Retriever 的边界写清楚
人工智能·python·开源
小九九的爸爸2 小时前
前端入门Agent开发,掌握这些Python数据基础就够啦
python·agent
风之所往_2 小时前
Python 3.9 新特性全面总结
python
W是笔名2 小时前
python___容器类型的数据___序列
开发语言·python