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>
7
  • Check this post . Commented Jun 10, 2021 at 12:58
  • @Swati, in my case, will I have = after my url? Commented Jun 10, 2021 at 13:02
  • 1
    = after my url.. Please elaborate . Also , i think this should work : "{% url 'update_status' 1234 %}".replace(/1234/, app_id.toString()) Commented Jun 10, 2021 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? Commented Jun 10, 2021 at 13:25
  • This answer should give you more info :) . Commented Jun 10, 2021 at 13:29

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.