解决方案:settings.py中加入 :SESSION_SERIALIZER = 'django.contrib.sessions.serializers.PickleSerializer'
事由:Django去访问一个web api接口,两次连接之间需要通过Session()保持身份验证。
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'] = httprequest.session['httpMobile'] = mobilereturn resultdef 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' 后解决。