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

133
Visualizações
Can't respond to AJAX call on context processor

I'm developing a website on DJANGO and I'm facing a problem:

I have a button in my website that, when triggered, sends an AJAX POST request. Since this button is in multiple places of my website, i've decided to receive the AJAX request on the context processor file of my backend.

This is what the request in the frontend looks like:

$.ajax({
    method: 'POST',
    headers: {'X-CSRFToken': csrftoken},
    data: {
        "requested_item": requested_item
    },
    dataType: "json",
    success: function() {
      // Handle success
    }
  });

And this is what the request handling looks like in the backend:

requested_item = request.POST.get('requested_item')
    if requested_item != None:
      # Handle valid request
      return JsonResponse({'success': True})

The thing is, the return JsonResponse({'success': True}) isn't working, it throws this error:

File "*path*\virtual-env\lib\site-packages\django\template\context.py", line 244, in bind_template
    updates.update(processor(self.request))
ValueError: dictionary update sequence element #0 has length 17; 2 is required

The data arrives without any trouble from the frontend to the backend, but I can't seem to respond.

I'm handling multiple AJAX requests the same way in my backend and responsing without problems, but this is the only one I handle in the context processor, so I'm sure there's something I'm missing.

Thanks!

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I eventually, with help, figured it out.

The context processor should only be used for template context or template rendering in general, therefore the response should be a dictionary, not a json type one (that's what the error's about).

I created an independent view for this AJAX request and specified in the frontend side of the AJAX the URL of this view and that way it worked.

about 4 years ago · Juan Pablo Isaza 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