【已解决】Flask项目报错AttributeError: ‘Request‘ object has no attribute ‘is_xhr‘

文章目录

报错及分析

报错代码

python 复制代码
  File "/www/kuaidi/6f47274023d4ad9b608f078c76a900e5_venv/lib/python3.6/site-packages/flask/json.py", line 251, in jsonify
    if current_app.config['JSONIFY_PRETTYPRINT_REGULAR'] and not request.is_xhr:
AttributeError: 'Request' object has no attribute 'is_xhr'

分析

这个问题是后端代码中的问题。

根据错误日志,'Request' 对象没有属性 'is_xhr'。这是因为在较新的 Flask 版本中,'is_xhr' 属性已被废弃。为了解决这个问题,可以使用 'is_ajax' 属性来代替 'is_xhr'

可以将代码中的 not request.is_xhr 改为 not request.is_ajax,这样应该可以解决这个错误。

将以下部分:

python 复制代码
if current_app.config['JSONIFY_PRETTYPRINT_REGULAR'] and not request.is_xhr:

更改为:

python 复制代码
if current_app.config['JSONIFY_PRETTYPRINT_REGULAR'] and not request.is_ajax:

此时发现代码中并没有这句,其实这句代码是在flask代码中。比如观察本报错,路径为/www/kuaidi/6f47274023d4ad9b608f078c76a900e5_venv/lib/python3.6/site-packages/flask/json.py,修改这个文件中的对应行即可。

python 复制代码
  File "/www/kuaidi/6f47274023d4ad9b608f078c76a900e5_venv/lib/python3.6/site-packages/flask/json.py", line 251, in jsonify
    if current_app.config['JSONIFY_PRETTYPRINT_REGULAR'] and not request.is_xhr:
AttributeError: 'Request' object has no attribute 'is_xhr'

解决方案

必要的解决方法

将文件(文件路径看具体报错)
/www/kuaidi/6f47274023d4ad9b608f078c76a900e5_venv/lib/python3.6/site-packages/flask/json.py中的

python 复制代码
if current_app.config['JSONIFY_PRETTYPRINT_REGULAR'] and not request.is_xhr:

更改为:

python 复制代码
if current_app.config['JSONIFY_PRETTYPRINT_REGULAR'] and not request.is_ajax:

可能有用的解决方法

观察库的版本是否合适,比如调整Flask库,Werkzeug库。

相关推荐
hboot4 分钟前
AI工程师第四课 - 深度学习入门
pytorch·python·神经网络
ZhengEnCi11 小时前
P2M-Matplotlib折线图完全指南-从数据可视化到趋势分析的Python绘图利器
python·matlab·数据可视化
ZhengEnCi12 小时前
P2L-Matplotlib饼图完全指南-从数据可视化到图表定制的Python绘图利器
python·matlab
曲幽12 小时前
你的REST接口还在“过度投喂”数据吗?——FastAPI + GraphQL实战避坑指南
python·fastapi·web·graphql·route·cors·rest·strawberry
CYY9513 小时前
OkHttp 和 Retrofit 封装使用
okhttp·retrofit
用户83580861879113 小时前
基于 Self-RAG 与列表级重排序的进阶 RAG 系统设计与实现
python
Warson_L1 天前
Python `Annotated` 与 LangGraph Reducer 学习笔记
python
韩师傅1 天前
海天线算法的前世今生
python·计算机视觉
韩师傅1 天前
当你的甲方设备过烂,要如何快速出效果?
python·计算机视觉
Warson_L1 天前
LangGraph的MessageState and HumanMessage
python