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

相关推荐
开飞机的舒克_1 小时前
FastAPI 实战入门:从路由、参数校验到依赖注入的后端开发指南
python·fastapi
PixelBai1 小时前
JSON查看器实际应用场景案例
json
霸道流氓气质1 小时前
Kiro 中反编译 JAR 包并分析字节码的流程指南
chrome·python·jar
人工智能时代 准备好了吗1 小时前
AI回答内容进入率监测:引用识别、文本匹配与语义判断
开发语言·人工智能·python
田里的水稻1 小时前
EP_XML\JSON配置文件和YAML
xml·运维·人工智能·机器人·自动驾驶·json
Metaphor6922 小时前
使用 Python 冻结 Excel 文件中的行、列和单元格
开发语言·python·excel
言乐62 小时前
Python实现建造微服务商城后台
开发语言·python·算法·微服务·架构
fenglllle3 小时前
chromadb emmbedding 向量检索
人工智能·python·embedding
cui_ruicheng3 小时前
Python从入门到实战(六):非序列容器
开发语言·python
百里香酚兰3 小时前
【python学习笔记】pyttsx3库疑似只播报一次语音
笔记·python·学习