0

I am trying to delete an object. This is the HTML, todo should be deleted when you Click on button (I am trying to call delete_todo) :-

<ul>
          {% for all %}


        </ul>

This is the views.py,

2 Answers 2

1

You need to change few things in your code. First of all change urlpattern delete_todo you need to add argument, which allows to determine in view what object you want to delete:

url(r'^(?P<todo_id>[0-9]+)/$', views.delete_todo, name='delete_todo'),

Then you need change delete_todo itself:

def delete_todo(request, todo_id):
    instance = get_object_or_404(Todo, pk=todo_id)
    instance.delete()
    return redirect('index')

Here you can use get_object_or_404 fuction to get object with id.

And finally you need to pass url's argument to view from template:

<form action="{% url 'lists:delete_todo' todo_id=todo.id %}" method=post>
      <input id="submit" type="button" value="Click" />
</form>
Sign up to request clarification or add additional context in comments.

2 Comments

Just changed it, I am clicking on the button, but nothing is happening.
I got it, It was related to CSRF verification. Thanks a lot.
0

Just to add clarification on the use of the form and csrf: it's necessary in order to ensure that different users of your app can't delete content that isn't theirs.

In your template, you'll need to include the csrf tag as such:

  <form method="post" action={% url 'delete_todo' todo_id=todo.id %}>
    {% csrf_token %}
    <input type="button" id="submit" value="Delete" />
  </form>

Comments

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.