【已解决】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库。

相关推荐
weixin_BYSJ198713 分钟前
springboot校园自习室管理小程序---附源码32142
java·javascript·spring boot·python·django·flask·php
用户83562907805144 分钟前
使用 Python 在 PDF 中绘制线条、矩形和自定义图形
后端·python
wang_yb1 小时前
用方差阈值过滤掉“惰性特征”
python·ai·databook
RFID固定资产管理系统3 小时前
公司RFID管理系统揭秘
大数据·python
IVEN_3 小时前
Python官方包、Conda、uv,应该怎么选
python
凡尘——雨落凡尘4 小时前
Python列表索引越界IndexError问题深度解析与解决办法
python·indexerror·list index out of range
Metaphor6924 小时前
使用 Python 在 Word 文档中添加或删除文本框
python·word
郝同学今天有进步吗4 小时前
构建 LangGraph Code Review Agent(七):实现规则匹配、Finding Guardrails 与 Markdown 报告
python·ai·fastapi·code review
xuhe25 小时前
一劳永逸!解决 AutoDL 系统盘(30GB)爆满与 pip 缓存迁移
linux·python·ai·jupyter
CodexDave5 小时前
Python 自动化接单实战(一):把 CSV 需求做成配置驱动解析器
java·python·自动化·json·数据清洗·python自动化·csv处理