2

I am new in django and I have following issue after running: Reverse for 'delete_task/{{todo.id}}' not found. 'delete_task/{{todo.id}}' is not a valid view function or pattern name.

In my template file index.html I have:

<form action="{% url 'delete_task/todo.id'%}" method="post" class = 'delete-link'>
    {% csrf_token%}
    <button type="submit">Delete</button>
</form>

My urls.py:

urlpatterns = [
path('delete_task/<int:todo_id>', views.delete_task, name='delete_task'),

]

My views.py:

def delete_task(request, task_id):
    return HttpResponse(task_id)
#the delete_task block is just for test

Thank you

Yespa16
  • 51
  • 4

2 Answers2

3

The name of the path is delete_task, so the first parameter of the {% url … %} template tag [Django-doc] is 'delete_task, then the second is the parameter (here todo.pk):

<form action="{% url 'delete_task' todo.id %}" method="post" class = 'delete-link'>
    {% csrf_token%}
    <button type="submit">Delete</button>
</form>
Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
2

Write like this:

<form action="{% url 'delete_task' todo.id %}" method="post" class = 'delete-link'>
{% csrf_token%}
<button type="submit">Delete</button>

Problem Solve

Shatish Desai
  • 575
  • 6
  • 20