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

295
Views
How to send django url with parameters to ajax request?

Currently, I am sending url to the ajax as :

    $('.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);
            }
        }
            }
  });

Currently, I am sending request into a static url. Can we use {% url toggle i.id %} to set the url dynamically in the ajax request? Does this ajax function repeats at all or not?

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

0

Not in that way or fashion, the {% url toggle i.id %} is for the Django template, so that is executed when rendering the page server side. What you want to do is client side.

Your are also trying to use "switch_{{forloop.counter}}", which won't work unless you have multiple snippets of the 'click' function. Which I would advise against since it simply doesn't make sense. You define a function once and then use it. See first example.

Best thing I can think of is exposing a 'base url' in your template and use that in javascript. For example in your Django template:

<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>

In your Django view you do something like this:


# 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!