Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

289
Views
¿Cómo enviar la URL de django con parámetros a la solicitud de ajax?

Actualmente, estoy enviando url a ajax como:

 $('.togglebtn').on("click",function(){ console.log("Hello") $.ajax({ type: 'GET', url: "http://localhost:8000/toggle/2945016571198", 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); } } } });

Actualmente, estoy enviando una solicitud a una URL estática. ¿Podemos usar {% url toggle i.id %} para establecer la url dinámicamente en la solicitud ajax? ¿Esta función ajax se repite o no?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

No de esa manera o moda, el {% url toggle i.id %} es para la plantilla de Django, por lo que se ejecuta al representar el lado del servidor de la página. Lo que quieres hacer es del lado del cliente.

También está intentando usar "switch_{{forloop.counter}}" , que no funcionará a menos que tenga varios fragmentos de la función 'clic'. Lo cual desaconsejaría ya que simplemente no tiene sentido. Usted define una función una vez y luego la usa. Ver primer ejemplo.

Lo mejor que se me ocurre es exponer una 'url base' en su plantilla y usarla en javascript. Por ejemplo, en su plantilla de Django:

 <script> // Put the Django variable into javascript context. toggle_base_url = {{ toggle_base_url_from_django_view_context }}; // Use it in your ajax url by combining strings. $('.togglebtn').on("click",function(){ console.log("Hello") var obj = $(this); // Save the clicked button for reference. var objectId = obj.attr('id'); var buttonId = obj.attr('data-button-id'); $.ajax({ type: 'GET', url: toggle_base_url + "/" + buttonId contentType: 'application/json', success: function (data) { // Cannot use 'this' in this context (IIRC), use the obj var we assigned. // Replace with something to find your parent container // Could use have a attribute on your button that points to the container. // Probably the number you use in the url? var mainContainer = obj.parent('containerclass'); 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>

En tu vista de Django, haces algo como esto:

 # Make a base url to reuse, remove the last part. # So turn `/toggle/1` into `/toggle` base_url = reverse('toggle', args=[1]).rsplit('/', 1) context = { 'toggle_base_url_from_django_view_context': base_url, } return render(request, 'yourtemplate.html', context=context)
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!