Refs #30997 -- Removed HttpRequest.is_ajax() usage.

This commit is contained in:
Claude Paroz 2019-12-15 15:30:35 +01:00 committed by Mariusz Felisiak
parent 5d654e1e71
commit 7fa0fa45c5
7 changed files with 47 additions and 30 deletions

View file

@ -388,6 +388,11 @@ Miscellaneous
Django 3.1, the first request to any previously cached template fragment will
be a cache miss.
* The logic behind the decision to return a redirection fallback or a 204 HTTP
response from the :func:`~django.views.i18n.set_language` view is now based
on the ``Accept`` HTTP header instead of the ``X-Requested-With`` HTTP header
presence.
* The compatibility imports of ``django.core.exceptions.EmptyResultSet`` in
``django.db.models.query``, ``django.db.models.sql``, and
``django.db.models.sql.datastructures`` are removed.

View file

@ -1803,9 +1803,18 @@ redirect to that URL will be performed. Otherwise, Django may fall back to
redirecting the user to the URL from the ``Referer`` header or, if it is not
set, to ``/``, depending on the nature of the request:
* For AJAX requests, the fallback will be performed only if the ``next``
parameter was set. Otherwise a 204 status code (No Content) will be returned.
* For non-AJAX requests, the fallback will always be performed.
* If the request accepts HTML content (based on its ``Accept`` HTTP header),
the fallback will always be performed.
* If the request doesn't accept HTML, the fallback will be performed only if
the ``next`` parameter was set. Otherwise a 204 status code (No Content) will
be returned.
.. versionchanged:: 3.1
In older versions, the distinction for the fallback is based on whether the
``X-Requested-With`` header is set to the value ``XMLHttpRequest``. This is
set by the jQuery ``ajax()`` method.
Here's example HTML template code:

View file

@ -159,11 +159,11 @@ Use the ``django.test.Client`` class to make requests.
>>> c = Client()
>>> c.get('/customers/details/', {'name': 'fred', 'age': 7},
... HTTP_X_REQUESTED_WITH='XMLHttpRequest')
... HTTP_ACCEPT='application/json')
...will send the HTTP header ``HTTP_X_REQUESTED_WITH`` to the
details view, which is a good way to test code paths that use the
:meth:`django.http.HttpRequest.is_ajax()` method.
...will send the HTTP header ``HTTP_ACCEPT`` to the details view, which
is a good way to test code paths that use the
:meth:`django.http.HttpRequest.accepts()` method.
.. admonition:: CGI specification