title: Delete object on click
body: <p>I am trying to make a todo app, I have a form with a table where the user can add "todo" to this form.</p>
  
  <p>I would like to be able to delete the added "todo" from database by clicking on a button without having to redirect the user to another page, is there any way to do this with Django?</p>
  
  <p><a href="http://postimg.org/image/8dpe1dcob/" rel="nofollow">http://postimg.org/image/8dpe1dcob/</a></p>
  
  <p>Views.py</p>
  
  <pre><code>from django.shortcuts import render
  from django.utils import timezone
  from .models import Task
  from .forms import TaskForm
  from django.shortcuts import redirect
  from django.http import HttpResponseRedirect
  from django.shortcuts import get_object_or_404
  from django.core.urlresolvers import reverse
  
  # Create your views here.
  
  def tasks_list(request):
      tasks = Task.objects.filter(created_date__lte=timezone.now()).order_by('created_date')
      form = TaskForm()
      if request.method == "POST":
          form = TaskForm(request.POST)
          if form.is_valid():
              post = form.save(commit=False)
              post.created_date = timezone.now()
              post.save()
              #return render(request, 'todo/tasks_list.html', {'tasks': tasks, 'form': form})
              return HttpResponseRedirect('/')
      else:
          form = TaskForm()
      return render(request, 'todo/tasks_list.html', {'tasks': tasks, 'form': form})
  
  
  
  
  def post_remove(request, new_id):
      post = get_object_or_404(Task, id=new_id)
      post.delete()
      return redirect('todo.views.tasks_list')
  </code></pre>
  
  <p>url.py</p>
  
  <pre><code>from django.conf.urls import url
  from . import views
  
  urlpatterns = [
      url(r'^$', views.tasks_list, name='tasks_list'),
      #url(r'^$', views.tasks_list, name='post_remove'),
  ]
  </code></pre>
  
  <p>form.py</p>
  
  <pre><code>from django import forms
  from .models import Task
  
  class TaskForm(forms.ModelForm):
      title = forms.CharField(widget=forms.TextInput(attrs={'placeholder': 'Add todo'}), label='')
      due_date = forms.DateField(widget=forms.DateInput(attrs={'placeholder': 'Add due date', 'class':'datepicker'}), label='')
  
      class Meta:
          model = Task
          fields = ('title', 'due_date',)
  </code></pre>
  
  <p>template/task_list.html</p>
  
  <pre><code>{% extends 'todo/base.html' %}
  {% load bootstrap %}
  
  {% block content %}
  &lt;h1&gt;Todos&lt;/h1&gt;
  &lt;div class="container" id="container"&gt;
    &lt;div class="input-group"&gt;
      &lt;!--&lt;input type="text" class="form-control" id="textField" placeholder="Add tasks"&gt;--&gt;
      &lt;form method="POST" class="post-form" &gt;{% csrf_token %}
        {{ form|bootstrap }}
  
        &lt;span class="input-group-btn"&gt;
          &lt;button class="btn btn-default" type="submit" id="plusSign"&gt;Go!&lt;/button&gt;
        &lt;/span&gt;
        &lt;/form&gt;
    &lt;/div&gt;&lt;!-- /input-group --&gt;
    &lt;table style="width:100%" id='todoTable'&gt;
      &lt;thead id="tableHead"&gt;
        &lt;tr&gt;
          &lt;td&gt;Todo&lt;/td&gt;
          &lt;td&gt;due date&lt;/td&gt;
          &lt;td&gt;delete&lt;/td&gt;
        &lt;/tr&gt;
      &lt;/thead&gt;
      &lt;tbody&gt;
      {% for task in tasks %}
      &lt;tr&gt;
        &lt;td&gt;{{ task.title }}&lt;/td&gt;
        &lt;td&gt; {{ task.due_date }} &lt;/td&gt;
        &lt;td&gt; &lt;a class="btn btn-default" href="{% url 'tasks_list' %}"&gt;&lt;span class="glyphicon glyphicon-remove"&gt;&lt;/span&gt;&lt;/a&gt; &lt;/td&gt;
      &lt;/tr&gt;
    &lt;/body&gt;
      {% endfor %}
    &lt;/table&gt;
  &lt;/div&gt;
  {% endblock %}
  </code></pre>
  
  <p>Edit:</p>
  
  <p>I found a solution using the code from the Django girls tutorial.</p>
  
  <p>I have added the following code:</p>
  
  <p>Views.py</p>
  
  <pre><code>def post_remove(request, pk):
      post = get_object_or_404(Task, pk=pk)
      post.delete()
      return HttpResponseRedirect('/')
  </code></pre>
  
  <p>Url.py</p>
  
  <pre><code>url(r'^task/(?P&lt;pk&gt;\d+)/remove/$', views.post_remove, name='post_remove'),
  </code></pre>
  
  <p>templates/task_list.html</p>
  
  <pre><code>&lt;td&gt; &lt;a class="btn btn-default" href="{% url 'post_remove' pk=task.pk %}"&gt;&lt;span class="glyphicon glyphicon-remove"&gt;&lt;/span&gt;&lt;/a&gt;&lt;/td&gt;
  </code></pre>
  
  <p>Could anyone check if my understand of the deletion is correct?</p>
  
  <p>So the <code>task_list.html</code> is adding a link with the primary key to each todo as <code>href="/task/49/remove/"</code> which allows the post_remove function in <code>views.py</code> to remove only the "todo" with the specific href and the removal only works when the button is clicked because of the request parameter at the post_remove function?</p>