30๐
Check HTTP_X_REQUESTED_WITH
header
def sample_view(request):
is_ajax = request.META.get('HTTP_X_REQUESTED_WITH') == 'XMLHttpRequest'
From the 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 newHttpRequest.accepts()
method if your code depends on the client Accept HTTP header.
8๐
Funny thing โ the quoted deprecation blurb only gets you halfway there. Thereโs no indication of how you "use the new HttpRequest.accepts
method" to replace HttpRequest.is_ajax
โ not in the deprecation text, nor the documentation, nor the release notes.
So, here it is: if request.accepts("application/json")
(At least thatโs what worked for me.)
- [Django]-What is the django template tag to get the number of items returned in a result set?
- [Django]-Best way to integrate SqlAlchemy into a Django project
- [Django]-A left outer reverse select_related in Django?
6๐
Instead of:
if request.is_ajax():
Helped me:
if request.headers.get('x-requested-with') == 'XMLHttpRequest':
- [Django]-How to set current user to user field in Django Rest Framework?
- [Django]-Django โ How to prepopulate admin form fields
- [Django]-Django โ two views, one page
3๐
Combining the suggestions works for our use casesโฆ
def is_ajax(request: django.http.request.HttpRequest) -> bool:
"""
https://stackoverflow.com/questions/63629935
"""
return (
request.headers.get('x-requested-with') == 'XMLHttpRequest'
or request.accepts("application/json")
)
And then replace all instances of request.is_ajax()
with is_ajax(request)
.
- [Django]-Django Get All Users
- [Django]-Django: Get model from string?
- [Django]-Django-AttributeError 'User' object has no attribute 'backend' (Butโฆ.it does?)
0๐
have you tried to check HttpRequest.headers?
HttpRequest.is_ajax() depends on HTTP_X_REQUESTED_WITH header.
so you can check this header, if it is true it would be AJAX other wise it would be a request from a browser.
HttpRequest.headers['HTTP_X_REQUESTED_WITH']
- [Django]-Django admin: separate read-only view and change view
- [Django]-Django filter older than day(s)?
- [Django]-Django: __in query lookup doesn't maintain the order in queryset
0๐
I did what arakkal-abu said but I also added
'X-Requested-With'
header with the same value
ie. 'XMLHttpRequest'
to my request and it worked
- [Django]-Is there a way to pass a variable to an 'extended' template in Django?
- [Django]-Django ListView โ Form to filter and sort
- [Django]-What is a "Manager" in django?
0๐
Make sure to import this at the top
import re
from django.http import JsonResponse
from django.utils.translation import gettext_lazy as _
from django.conf.urls import handler404
You can have this inside your function/method to determine if from browser or ajax call
requested_html = re.search(r'^text/html', request.META.get('HTTP_ACCEPT'))
if requested_html:
# requested from browser, do as per your wish
# ajax call. Returning as per wish
return JsonResponse({
'detail': _('Requested API URL not found')
}, status=404, safe=False)
Explanation
If you request to load a page from a browser, you would see in the network tab under the requested headers of that request, text/html
is at the beginning of requested headers
.
However, if you are making an ajax call from the browser, the requested headers
has */*
in the beginning. If you attach
Accept: application/json
in the header, then requested headers become this
From this, you can understand how the accept header is different in these cases.
- [Django]-How can I get the file name from request.FILES?
- [Django]-Django: timezone.now vs timezone.now()
- [Django]-"Too many SQL variables" error in django with sqlite3