1 回答
TA贡献1784条经验 获得超8个赞
在表中显示记录时,需要使用“删除表单”按钮添加额外的表数据。例如:
<table>
{% for item in items%}
<tr>
<td>{{item.id}}</td>
<td>{{item.name}}</td>
<td>
<form method="delete">
<input type="hidden" value="{{item.id}}" name="delete-id">
<input type="Submit">Delete</button>
</form>
</td>
</tr>
{% endfor}
</table>
现在,在 views.py 添加删除部分并删除记录:
if request.method == 'DELETE':
Person.objects.get(pk=request.DELETE['delete-id']).delete()
您正在使用django_tables2,您只需将此代码添加到 tables.py
from django_tables2.utils import A # alias for Accessor
class PersonTable(tables.Table):
delete = tables.LinkColumn('main:delete_item', args=[A('delete-id')], attrs={
'a': {'class': 'btn'}
})
添加回答
举报