'ASGIRequest' object has no attribute 'is_ajax'

来源:6-6 用户发表动态

weixin_慕仙3585306

2023-07-29

AttributeError at /news/post-news/
‘ASGIRequest’ object has no attribute ‘is_ajax’
Request Method: GET
Request URL: http://localhost:8000/news/post-news/
Django Version: 4.1.9
Exception Type: AttributeError
Exception Value:
‘ASGIRequest’ object has no attribute ‘is_ajax’
Exception Location: /app/zanhu/helper.py, line 10, in wrap
Raised during: zanhu.news.views.post_new
Python Executable: /usr/local/bin/python
Python Version: 3.11.4
Python Path:
[’/app’,
’/app’,
’/usr/local/bin’,
’/usr/local/lib/python311.zip’,
’/usr/local/lib/python3.11’,
’/usr/local/lib/python3.11/lib-dynload’,
’/usr/local/lib/python3.11/site-packages’,
’/app/zanhu’]
Server time: Sat, 29 Jul 2023 18:54:28 +0800

写回答

2回答

Jack

2023-08-06

hi, 课程用的Django版本比较早了,应该是版本的问题。较新的Django版本中,is_ajax属性已被弃用。试试使用is_ajax()  使用request.is_ajax()来检查请求是否为Ajax请求

1
0

weixin_慕仙3585306

提问者

2023-07-29

This exception is directly caused by the deprecation of  `is_ajax` since Django 3.1

Use another way to detect AJAX requests.

```

if request.headers.get('X-Requested-With') == 'XMLHttpRequest':
    is_ajax = True
 else:
    is_ajax = False

```

0
0

Django高级实战 开发企业级问答网站

融合Django高级用法/算法/设计模式/TestCase测试/云计算打造项目

900 学习 · 756 问题

查看课程