Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

535
Visualizações
AttributeError: 'WSGIRequest' object has no attribute 'is_ajax'

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>
over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

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 new HttpRequest.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)
over 4 years ago · Santiago Trujillo Relatório

0

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.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda