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

相关推荐
乘风破浪的小太阳7 分钟前
Python之Playwright+AI UI自动化测试框架搭建与实战
人工智能·python·ui
chushiyunen11 分钟前
pycharm打包whl
人工智能·pytorch·python
墨染天姬12 分钟前
【AI】PyTorch 框架
人工智能·pytorch·python
AI+程序员在路上14 分钟前
在pyCharm 中命令打包生成exe文件方法
ide·python·pycharm
nimadan1226 分钟前
海螺口型同步2025解析,提升多设备协同效率的三大核心策略
人工智能·python
KIHU快狐26 分钟前
KIHU快狐|vr全景体验一体机高配置硬件带来更流畅操作体验
python
阿轲Jackson29 分钟前
AttributeError: ‘WebDriver‘ object has no attribute ‘find_element_by_id‘
python·selenium
喵手36 分钟前
Python爬虫实战:Apple Music华语榜每日增量追踪与峰值计算!
爬虫·python·爬虫实战·零基础python爬虫教学·apply music·华语榜单·每日增量与峰值计算
烟锁池塘柳043 分钟前
【已解决】解决 ModuleNotFoundError: No module named ‘exceptions‘
python·pip
Lw中1 小时前
RAG切片语义割裂怎么办?
python·rag文本分割·大模型应用基础