我有这个错误 delete() missing 1 required positional argument: 'id' 当我点击删除按钮
I have this error delete() missing 1 required positional argument: 'id' when I click on the delete button
当我点击删除按钮时,我应该将它从我的数据库中删除,但我却得到了这个错误:delete() missing 1 required positional argument: 'id' 如图所示以下:
urls.py
urlpatterns = [
path('', views.login_user, name='login'),
path('home/', views.home, name='home'),
path('allstaff/', views.allstaff, name='allstaff'),
#path('delete_order/<str:pk>/', views.deleteOrder, name="delete_order"),
path('delete', views.delete, name='delete'),
path('logout/', views.logout_view, name='logout'),
path('register/', views.register_view, name='register'),
path('edit-register/', views.edit_register_view, name='edit_register'),
]
views.py
def delete(request, id):
context = {}
user = get_object_or_404(User, id=id)
if request.method == "POST":
user.delete(id)
return HttpResponseRedirect("/home")
return render(request, 'delete.html', context)
allstaff.py
{% extends "home.html" %}
{% block content %}
<style>
table {
border-collapse:separate;
border:solid black 1px;
border-radius:6px;
-moz-border-radius:6px;
}
td, th {
border-left:solid black 1px;
border-top:solid black 1px;
}
th {
border-top: none;
}
td:first-child, th:first-child {
border-left: none;
}
</style>
<div style="padding-left:16px">
<br>
<div class="form-block">
<table>
<tr>
<th>Staff Name</th>
<th>Staff Username</th>
<th>Email</th>
<th>Date Joined</th>
<th>Action</th>
</tr>
{% for user in allusername %}
<tr>
<td>{{user.first_name}} {{user.last_name}}</td><td>{{user.username}}</td><td>{{user.email}}</td><td>{{user.date_joined}}</td><td><a class="btn btn-sm btn-info" href="{}">Update</a></td><td><a class="btn btn-sm btn-danger" href="/delete">Delete</a></td>
</tr>
{% endfor %}
</table>
<br>
</div>
</div>
{% endblock %}
我错过了什么?我如何让它工作,以便当我点击删除按钮时,它会自动从我的数据库中删除?
URL 应该包含删除该用户的主键(或其他唯一)字段,因此:
path('delete/<strong><int:id>/</strong>', views.delete, name='delete'),
然后在模板中,我们制作一个迷你表单,其中包含要删除的 User
对象的 id
:
{% for user in allusername %}
…
<td>
<form action="<strong>{% url 'delete' user.id %}</strong>" method="post">
{% csrf_token %}
<button type="submit" class="btn btn-sm btn-danger">Delete</button>
</form>
</td>
…
{% endfor %}
最后,在您看来,您删除了 User
对象 而没有再次指定 ID:您已经有了正确的对象,因为您使用 User
获取了id=id
:
from django.shortcuts import redirect
def delete(request, id):
user = get_object_or_404(User, id=id)
if request.method == 'POST':
user.delete()
return redirect('home')
return render(request, 'delete.html')
当我点击删除按钮时,我应该将它从我的数据库中删除,但我却得到了这个错误:delete() missing 1 required positional argument: 'id' 如图所示以下:
urls.py
urlpatterns = [
path('', views.login_user, name='login'),
path('home/', views.home, name='home'),
path('allstaff/', views.allstaff, name='allstaff'),
#path('delete_order/<str:pk>/', views.deleteOrder, name="delete_order"),
path('delete', views.delete, name='delete'),
path('logout/', views.logout_view, name='logout'),
path('register/', views.register_view, name='register'),
path('edit-register/', views.edit_register_view, name='edit_register'),
]
views.py
def delete(request, id):
context = {}
user = get_object_or_404(User, id=id)
if request.method == "POST":
user.delete(id)
return HttpResponseRedirect("/home")
return render(request, 'delete.html', context)
allstaff.py
{% extends "home.html" %}
{% block content %}
<style>
table {
border-collapse:separate;
border:solid black 1px;
border-radius:6px;
-moz-border-radius:6px;
}
td, th {
border-left:solid black 1px;
border-top:solid black 1px;
}
th {
border-top: none;
}
td:first-child, th:first-child {
border-left: none;
}
</style>
<div style="padding-left:16px">
<br>
<div class="form-block">
<table>
<tr>
<th>Staff Name</th>
<th>Staff Username</th>
<th>Email</th>
<th>Date Joined</th>
<th>Action</th>
</tr>
{% for user in allusername %}
<tr>
<td>{{user.first_name}} {{user.last_name}}</td><td>{{user.username}}</td><td>{{user.email}}</td><td>{{user.date_joined}}</td><td><a class="btn btn-sm btn-info" href="{}">Update</a></td><td><a class="btn btn-sm btn-danger" href="/delete">Delete</a></td>
</tr>
{% endfor %}
</table>
<br>
</div>
</div>
{% endblock %}
我错过了什么?我如何让它工作,以便当我点击删除按钮时,它会自动从我的数据库中删除?
URL 应该包含删除该用户的主键(或其他唯一)字段,因此:
path('delete/<strong><int:id>/</strong>', views.delete, name='delete'),
然后在模板中,我们制作一个迷你表单,其中包含要删除的 User
对象的 id
:
{% for user in allusername %}
…
<td>
<form action="<strong>{% url 'delete' user.id %}</strong>" method="post">
{% csrf_token %}
<button type="submit" class="btn btn-sm btn-danger">Delete</button>
</form>
</td>
…
{% endfor %}
最后,在您看来,您删除了 User
对象 而没有再次指定 ID:您已经有了正确的对象,因为您使用 User
获取了id=id
:
from django.shortcuts import redirect
def delete(request, id):
user = get_object_or_404(User, id=id)
if request.method == 'POST':
user.delete()
return redirect('home')
return render(request, 'delete.html')