gpt4 book ai didi

Django >= 3.1 和 is_ajax

转载 作者:行者123 更新时间:2023-12-03 16:20:31 24 4
gpt4 key购买 nike

HttpRequest.is_ajax()从 3.1 版开始不推荐使用。
如果页面是从浏览器请求的,我想返回 html 并且作为 JsonResponse如果从 javascript 或以编程方式调用。
我正在寻求有关如何做到这一点的指导。
https://docs.djangoproject.com/en/3.1/ref/request-response/#django.http.HttpRequest.is_ajax

最佳答案

查询 HTTP_X_REQUESTED_WITH标题

def sample_view(request):
is_ajax = request.META.get('HTTP_X_REQUESTED_WITH') == 'XMLHttpRequest'
来自 Release Notes of 3.1

The HttpRequest.is_ajax() method is deprecated as it relied on a jQuery-specific way of signifying AJAX calls, while current usage tends to use the JavaScript Fetch API. Depending on your use case, you can either write your own AJAX detection method, or use the new HttpRequest.accepts() method if your code depends on the client Accept HTTP header.

关于Django >= 3.1 和 is_ajax,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63629935/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com