Reputation: 395
im trying to learn ajax in django but when i running this simple test i got this error and i cant find the reason, my django version is 4.0
AttributeError: 'WSGIRequest' object has no attribute 'is_ajax'
view.py
from django.shortcuts import render, HttpResponse
def home(request):
return render(request,'myapp/index.html')
def ajax_test(request):
if request.is_ajax():
message = "This is ajax"
else:
message = "Not ajax"
return HttpResponse(message)
urls.py
urlpatterns = [
path('',views.home,name='home'),
path('ajax_test/', views.ajax_test, name='ajax_test')
]
index.html
<button id="btn">click me!</button>
<script>
$("#btn").click(function () {
$.ajax({
type: "GET",
url: "{% url 'ajax_test' %}",
success: function () {
console.log("done");
},
error: function () {
console.log("error");
},
});
});
</script>
Upvotes: 24
Views: 24116
Reputation: 3121
this method should not be removed, since jQuery the most underrated library, still surviving, strongly, in many modern tutorials. hope one day they will bring it back.
And I found it weird for same version of django, after server deployment this error was reported, but never in local machine.
if you have implemented is_ajax
in your project, and it is largely scoped, here is a way to implement is_ajax
to every request
middlewares.py
in any of your app, in my case, common
app. (it does not matter which app you add this in, middlewares are wrapper functions called globally to perform action before or after view)class AjaxMiddleware:
def __init__(self, get_response):
self.get_response = get_response
def __call__(self, request):
def is_ajax(self):
return request.META.get('HTTP_X_REQUESTED_WITH') == 'XMLHttpRequest'
request.is_ajax = is_ajax.__get__(request)
response = self.get_response(request)
return response
this will define a is_ajax
method on every request before it is received by the view.
settings.py
:MIDDLEWARE = [
'common.middlewares.AjaxMiddleware',
]
Upvotes: 22
Reputation: 827
To complet the JPG answer based on the release notes of 3.1
request.is_ajax() can be reproduced exactly as request.headers.get('x-requested-with') == 'XMLHttpRequest'
The request
object used by Django is like requests. You can identify an AJAX request when your client use the specific header x-requested-with
set with XMLHttpRequest
.
Upvotes: 13
Reputation: 88619
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.
Even though it has been deprecated, you can create a custom function to check the request type as,
def is_ajax(request):
return request.META.get('HTTP_X_REQUESTED_WITH') == 'XMLHttpRequest'
and use anywhere in your view as,
from django.shortcuts import HttpResponse
def is_ajax(request):
return request.META.get('HTTP_X_REQUESTED_WITH') == 'XMLHttpRequest'
def ajax_test(request):
if is_ajax(request=request):
message = "This is ajax"
else:
message = "Not ajax"
return HttpResponse(message)
Upvotes: 48