0

I have the following code in a template ("home.html") in my Django project

function deleteData(toBeDeleted) {
        $.ajax({
            type: "GET",
            url: "{% url 'delete_data' **[Replace me]** 'all' %}",
            success: function (data) {
                alert(data)
            }
        }) 
    }

I need to reach the url 'delete_data' with 2 arguments where the 2nd argument is 'all' and the 1st argument is a javascript variable called "toBeDeleted" (Passed in as a function argument)

A work around i have thought about is...

            url: `/delete_data/$(toBeDeleted)/all`,

But in that case i will have to change that as well when changing the url for deleting data other than just changing the urls.py file

Is there a way to make this work or do i have to give up on using {% url ... %}?

  • Relevant: [What is the difference between client-side and server-side programming?](https://stackoverflow.com/q/13840429) – VLAZ Aug 23 '23 at 19:10
  • IMHO here you can find your answer to this: https://stackoverflow.com/questions/13938605/django-url-parameter-and-reverse-url – ger.s.brett Aug 24 '23 at 05:43

1 Answers1

0

Asked that on a tired night, next day I rethought about it and realised that this is not possible. The best alternative to that would be passing the URL string as the variable in the JS Function and not constructing it with a variable inside the function, as shown:

<button onclick="deletedata('{% url 'delete_data' 'A' 'all' %}')">Delete A</button>
<button onclick="deletedata('{% url 'delete_data' 'B' 'all' %}')">Delete B</button>

<script>
function deleteData(toBeDeletedURL) {
        $.ajax({
            type: "GET",
            url: toBeDeletedURL,
            success: function (data) {
                alert(data)
            }
        }) 
    }
</script>

Still if someone has a better answer you can add it and I will accept it but that's the best idea I found