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

244
Visualizações
How to pass django context values to the javascript variable?

I want to pass all the values of the {{ i.nsn }} turn by turn to the ajax script.

{% for i in dibbs %}

<p>{{ i.nsn }}</p>

{% endfor %}

If I tried the ajax script in the way

{% for i in dibbs %}
  <script>
      var nsn = {{ i.nsn }}
      console.log("Hello")
         $('.togglebtn').on("click",function(){
         console.log("Hello")
         $.ajax({
            type: 'GET',
            url: "http://localhost:8000/toggle/"+nsn,
            contentType: 'application/json',
            success: function (data) {
                  appendData(data);
                   function appendData(data) {
            var mainContainer = document.getElementById("switch_{{forloop.counter}}");
            for (var i = 0; i < data.length; i++) {
                var div = document.createElement("div");
                div.innerHTML = '<tr>' + data[i].line_items + ' ' + data[i].nomenclature+'</tr>' ;
                mainContainer.appendChild(div);
            }
        }
            }
  });
  });




      </script>

{% endfor %}

The ajax script also repeats as per the loop.

My url is different for every nsn. I want to send a single ajax request when I clicked in the button.

I want to execute a single ajax function for a single click removing the for loop.

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

0

I think you can try to fetch value from p tag and pass it to your ajax

{% for i in dibbs %}
  <p>{{ i.nsn }}</p>


  <script>
    var nsn = ctrl.getElementsByTagName('p')[0].innerHTML;
      console.log("Hello")
         $('.togglebtn').on("click",function(){
         console.log("Hello")
         $.ajax({
            type: 'GET',
            url: "http://localhost:8000/toggle/"+nsn,
            contentType: 'application/json',
            success: function (data) {
                  appendData(data);
                   function appendData(data) {
            var mainContainer = document.getElementById("switch_{{forloop.counter}}");
            for (var i = 0; i < data.length; i++) {
                var div = document.createElement("div");
                div.innerHTML = '<tr>' + data[i].line_items + ' ' + data[i].nomenclature+'</tr>' ;
                mainContainer.appendChild(div);
            }
        }
            }
  });
  });




      </script>

{% endfor %}

You can also hide the p tag if you don't want to display

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