Search code examples
htmldjangodjango-formsdjango-urlsdjango-url-reverse

Reverse for 'delete_task/{{todo.id}}' not found. 'delete_task/{{todo.id}}' is not a valid view function or pattern name


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


Solution

  • 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>