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 分钟前
Python技术深度探索:从基础到进阶的实践之旅(第一篇)
开发语言·python·运维开发·技术共享
Bonne journée3 分钟前
‌在Python中,print(f‘‘)是什么?
java·开发语言·python
iiimZoey27 分钟前
Dit架构 diffusion范式分类+应用
人工智能·python
R三哥哥啊28 分钟前
【树莓派5B】IO串口通信使用
python
互联网时光机1 小时前
python写的window小工具-一键设置ip自动获取ip
网络·windows·python
SCBAiotAigc1 小时前
VSCode debug模式无法跳转进入内置模块
人工智能·vscode·python
call_me_wangcheng1 小时前
django创建项目
python·django
VXbishe1 小时前
(附源码)基于springboot的“我来找房”微信小程序的设计与实现-计算机毕设 23157
java·python·微信小程序·node.js·c#·php·课程设计
chusheng18401 小时前
Python 如何使用 multiprocessing 模块创建进程池
python·信息可视化·matplotlib
猿小猴子1 小时前
Python3 爬虫 中间人爬虫
开发语言·爬虫·python