[Django]-Reverse for 'deletedata' with keyword arguments '{'pk': 1}' not found. 1 pattern(s) tried: ['delete/<int:id>/']

2👍

You are using re_path like it is path. re_path expects regex, it does not have path converters like path. You can either write a regex or switch to path.
Regex solution:

urlpatterns=[
    re_path(r'delete/(?P<pk>\d+)/',views.delete_data,name="deletedata")
]

path solution:

from django.urls import path

urlpatterns=[
    path('delete/<int:pk>/',views.delete_data,name="deletedata")
]

Edit: Also in your view you have named your parameter as id, all captured arguments from the url pattern are passed as keyword arguments, ensure that your naming is consistent in your pattern, view, template.
Change your view definition (and all usages of the variable in the view function to pk) to:

def delete_data(request, pk):

0👍

Try this

In the template, you have used pk

change in the template or in view and url

urlpatterns=[
    re_path('<int:pk>/',views.update_data,name="deletedata")
]

Or

<form action="{% url 'deletedata' id = st.id %}" method = "POST" class="d-inline"> {% csrf_token %}
        
        <input type="submit" class="btn btn-danger" value="Delete">
        </form>

0👍

{% url ‘deletedata’ pk = st.id %}
in thr URL just remove the spaces, bellow code it should work

    <tbody>
  {% for st in stu %}
    <tr>
      <th scope="row">{{st.id}}</th>
      <td>{{st.name}}</td>
      <td>{{st.email}}</td>
      <td>{{st.role}}</td> 
      <td>
        <a href="{}" class="btn btn-warning btn-sm">Edit</a>
        <form action="{% url 'deletedata' pk=st.id %}" method = "POST" class="d-inline"> {% csrf_token %}
        
        <input type="submit" class="btn btn-danger" value="Delete">
        </form>
          
      </td>
    </tr>
    {% endfor %}
  </tbody>
</table>

0👍

You can also write it with one a tag

    <tbody>
  {% for st in stu %}
    <tr>
      <th scope="row">{{st.id}}</th>
      <td>{{st.name}}</td>
      <td>{{st.email}}</td>
      <td>{{st.role}}</td> 
      <td>
        <a href="{}" class="btn btn-warning btn-sm">Edit</a>
        <a href="{% url 'deletedata' pk=st.id %}" class="d-inline">Delete</a>
        
        <input type="submit" class="btn btn-danger" value="Delete">
        </form>
          
      </td>
    </tr>
    {% endfor %}
  </tbody>
</table>

and then your views.py

def delete_data(request,id):
        pi = User.objects.get(pk=id)
        pi.delete()
        return HttpResponseRedirect('/')

Urls.py

urlpatterns=[
    path('<int:pk>/',views.delete_data,name="deletedata")
]

Leave a comment