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

相关推荐
weixin_4215850128 分钟前
PYTHON 迭代器1 - PEP-255
开发语言·python
hxxjxw1 小时前
Pytorch分布式训练/多卡训练(六) —— Expert Parallelism (MoE的特殊策略)
人工智能·pytorch·python
dagouaofei2 小时前
PPT AI生成实测报告:哪些工具值得长期使用?
人工智能·python·powerpoint
BoBoZz192 小时前
ExtractPolyLinesFromPolyData切割一个三维模型(球体),并可视化切割后产生的多条等高线
python·vtk·图形渲染·图形处理
quikai19812 小时前
python练习第六组
java·前端·python
Trouville012 小时前
Python中encode和decode的用法详解
开发语言·python
belldeep2 小时前
python:backtrader 使用指南
python·backtrader·量化回测
Dxy12393102163 小时前
Python的正则表达式如何做数据校验
开发语言·python·正则表达式
Daily Mirror3 小时前
Day38 MLP神经网络的训练
python