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>
- [Django]-How to POST data to remote server and display result
- [Django]-Which apps/solutions are most appropriate for model based search in Django?
- [Django]-Django translation: msgfmt: command not found
- [Django]-Python: Where to put external packages?
- [Django]-Ordering Filter using 2 ordering values(django_filters)
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>
- [Django]-Django app doesn't recognize static files
- [Django]-Django: How to update a class attribute of an invalid form field to display error messages in Bootstrap 5?
- [Django]-Django migration hell, dropped a table. Tried to get it back
- [Django]-What is fuzzy strings in gettext?
- [Django]-How to get media images in url in django url with reactjs
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")
]
Source:stackexchange.com