How do I send a JavaScript confirm response over Django to a script to perform operations based on the response?
<p>I'm very very new to Django and I'm currently trying to build a simple To-Do application. I'm currently able to display my To-Do list on the webpage using HTML Tables, but to be able to delete it, I added a new row at the end of every entry called <code>"Delete"</code> with an a <code>href element, on click event</code> sending it to a JS function that pops up a confirmation to the user asking if he actually wants to delete it. I'm storing the response in a variable called <code>response</code>. Now, my question is how do I send this response across django to actually be able to delete the entry? This is what I have done so far and its not working. All help will be appreciated. Thank You. </p>

<pre><code>{% for item in task %}
    &lt;tr&gt;
    &lt;td&gt;{{ item.task_name }}&lt;/td&gt;
    &lt;td&gt;{{ item.task_priority }}&lt;/td&gt;
    &lt;td&gt;{{ item.task_status }}&lt;/td&gt;
    &lt;td&gt;{{ item.target_date }}&lt;/td&gt;
    &lt;td&gt;&lt;a href="" onclick="return delete_function()"&gt;Delete&lt;/a&gt;&lt;/td&gt;
        &lt;p id="delete"&gt;&lt;/p&gt;
        &lt;/tr&gt;
    {% endfor %}

    &lt;/table&gt;
&lt;script&gt;
    function delete_function() {
        var response = confirm("Are you sure you want to delete this item?");
        if (response == true){
            response.post()
        }
    }
&lt;/script&gt;
</code></pre>

<p>In my views page, I have this function to handle this post method:</p>

<pre><code>def view_task(request):
    if request.method == 'GET':
        context = dict()
        context['task'] = Task.objects.all()
        return render(request, 'todo/view_tasks.html', context)

    if request.method == 'POST':
        task_name = request.post['task_name']
        Task.objects.delete(task_name)
        Task.objects.save()
</code></pre>

<p>What am I doing wrong? Also, could you also explain to me with your answer when and where should I use post and get or at least direct me to some very well answered questions about the same topic?</p>

<p>I don't think that this would be a good way to solve the problem, </p>

<p>try to do this:</p>

<p>in your template.html:</p>

<pre><code>&lt;form method="POST" action=""&gt;
    {% csrf_token %}
    &lt;input type="submit" name="delete_amount" onclick="delete_me(event)" /&gt;
&lt;/form&gt;
</code></pre>

<p>in your view.py:</p>

<pre><code>#just in case you have other form
if 'delete_amount' in request.POST:
    # do your deleting query here
    print('deleting amount')
</code></pre>

<p>and in your javascript.js:</p>

<pre><code>&lt;script&gt;
function delete_me(e)
{
     if(!confirm('Are you sure you want to delete this?')){
          //prevent sending the request when user clicked 'Cancel'
          e.preventDefault();
     }
}

&lt;/script&gt;
</code></pre>

<p>Hope this will help!</p>

javascript python django post get