Fixed #30997 -- Deprecated HttpRequest.is_ajax().

This commit is contained in:
Claude Paroz 2020-01-23 18:01:41 +01:00 committed by Mariusz Felisiak
parent 7fa0fa45c5
commit e348ab0d43
6 changed files with 31 additions and 1 deletions

View File

@ -1,6 +1,7 @@
import cgi import cgi
import codecs import codecs
import copy import copy
import warnings
from io import BytesIO from io import BytesIO
from itertools import chain from itertools import chain
from urllib.parse import quote, urlencode, urljoin, urlsplit from urllib.parse import quote, urlencode, urljoin, urlsplit
@ -15,6 +16,7 @@ from django.http.multipartparser import MultiPartParser, MultiPartParserError
from django.utils.datastructures import ( from django.utils.datastructures import (
CaseInsensitiveMapping, ImmutableList, MultiValueDict, CaseInsensitiveMapping, ImmutableList, MultiValueDict,
) )
from django.utils.deprecation import RemovedInDjango40Warning
from django.utils.encoding import escape_uri_path, iri_to_uri from django.utils.encoding import escape_uri_path, iri_to_uri
from django.utils.functional import cached_property from django.utils.functional import cached_property
from django.utils.http import is_same_domain, limited_parse_qsl from django.utils.http import is_same_domain, limited_parse_qsl
@ -256,6 +258,11 @@ class HttpRequest:
return self.scheme == 'https' return self.scheme == 'https'
def is_ajax(self): def is_ajax(self):
warnings.warn(
'request.is_ajax() is deprecated. See Django 3.1 release notes '
'for more details about this deprecation.',
RemovedInDjango40Warning,
)
return self.META.get('HTTP_X_REQUESTED_WITH') == 'XMLHttpRequest' return self.META.get('HTTP_X_REQUESTED_WITH') == 'XMLHttpRequest'
@property @property

View File

@ -44,6 +44,8 @@ details on these changes.
* The ``django-admin.py`` entry point will be removed. * The ``django-admin.py`` entry point will be removed.
* The ``HttpRequest.is_ajax()`` method will be removed.
See the :ref:`Django 3.1 release notes <deprecated-features-3.1>` for more See the :ref:`Django 3.1 release notes <deprecated-features-3.1>` for more
details on these changes. details on these changes.

View File

@ -431,6 +431,8 @@ Methods
.. method:: HttpRequest.is_ajax() .. method:: HttpRequest.is_ajax()
.. deprecated:: 3.1
Returns ``True`` if the request was made via an ``XMLHttpRequest``, by Returns ``True`` if the request was made via an ``XMLHttpRequest``, by
checking the ``HTTP_X_REQUESTED_WITH`` header for the string checking the ``HTTP_X_REQUESTED_WITH`` header for the string
``'XMLHttpRequest'``. Most modern JavaScript libraries send this header. ``'XMLHttpRequest'``. Most modern JavaScript libraries send this header.

View File

@ -609,7 +609,7 @@ Django 1.4 also includes several smaller improvements worth noting:
* A new, plain-text, version of the HTTP 500 status code internal error page * A new, plain-text, version of the HTTP 500 status code internal error page
served when :setting:`DEBUG` is ``True`` is now sent to the client when served when :setting:`DEBUG` is ``True`` is now sent to the client when
Django detects that the request has originated in JavaScript code. Django detects that the request has originated in JavaScript code.
(:meth:`~django.http.HttpRequest.is_ajax` is used for this.) (``is_ajax()`` is used for this.)
Like its HTML counterpart, it contains a collection of different Like its HTML counterpart, it contains a collection of different
pieces of information about the state of the application. pieces of information about the state of the application.

View File

@ -471,6 +471,13 @@ Miscellaneous
* The ``django-admin.py`` entry point is deprecated in favor of * The ``django-admin.py`` entry point is deprecated in favor of
``django-admin``. ``django-admin``.
* The ``HttpRequest.is_ajax()`` method is deprecated as it relied on a
jQuery-specific way of signifying AJAX calls, while the current usage tends
to use the ``fetch()`` JavaScript API. Depending on your use case, you can
either write your own AJAX detection method, or use the new
:meth:`.HttpRequest.accepts` method if your code depends on the client
``Accept`` HTTP header.
.. _removed-features-3.1: .. _removed-features-3.1:
Features removed in 3.1 Features removed in 3.1

View File

@ -0,0 +1,12 @@
from django.http import HttpRequest
from django.test import SimpleTestCase, ignore_warnings
from django.utils.deprecation import RemovedInDjango40Warning
@ignore_warnings(category=RemovedInDjango40Warning)
class TestDeprecatedIsAjax(SimpleTestCase):
def test_is_ajax(self):
request = HttpRequest()
self.assertIs(request.is_ajax(), False)
request.META['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest'
self.assertIs(request.is_ajax(), True)