1

I have to make an ajax call from my template.html. The view takes in an id parameter which I have obtained from the jquery part. How do I append it to my url?

Thank you

Here is the code:

 <script>
  ..... #some codes 
    app_div_id = $(this).closest('div').attr('id')
    var index = app_div_id.lastIndexOf("_");
    var result = app_div_id.substr(index+1);
    var app_id = parseInt(result);        
  
    $.ajax({
        url:"{% url 'update_status' %}", #this url takes in an id param
        data:{
            'new_app_status':app_status,
        },
        success: function (response) {
         

        },
        error: function (response) {
            console.log(response.errors);
          },
        });
    });
</script>
Alpha
  • 237
  • 1
  • 2
  • 7
  • Check [this](https://stackoverflow.com/questions/17832194/get-javascript-variables-value-in-django-url-template-tag) post . – Swati Jun 10 '21 at 12:58
  • @Swati, in my case, will I have = after my url? – Alpha Jun 10 '21 at 13:02
  • 1
    *= after my url..* Please elaborate . Also , i think this should work : `"{% url 'update_status' 1234 %}".replace(/1234/, app_id.toString())` – Swati Jun 10 '21 at 13:05
  • @Swati, it worked. I wanted to clarify something. Do parameters in django urls such as id always get sent as a string? In my view, I'll have to convert it back to int then? – Alpha Jun 10 '21 at 13:25
  • [This](https://stackoverflow.com/a/40303864/10606400) answer should give you more info :) . – Swati Jun 10 '21 at 13:29
  • @Swati, thaanks :)). Having issues in the ajax call now :/ – Alpha Jun 10 '21 at 14:41
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/233624/discussion-between-swati-and-alpha). – Swati Jun 10 '21 at 15:46

0 Answers0