【发布时间】:2021-09-13 00:59:16
【问题描述】:
我有一个包含数据的表格和一个按钮,用户可以在其中删除整个对象,我实现了一个模板,用户应该在该模板中被重定向,以便他可以选择确认删除或取消删除。
但是当我单击删除按钮时,它会立即删除对象,知道如何告诉视图首先转到该模板,然后,如果是这种情况,删除对象?
这里是模板:borrar_oficio.html我想在点击删除按钮后被重定向:
{% extends 'base.html' %}
{% block content %}
<p>Are you sure you want to delete {{ item }}?</p>
<form action="{% url 'oficios:borrar' item.id %}" method="POST">
{% csrf_token %}
<a href="{% url 'oficios:list' %}">Cancelar</a>
<input type="submit" name="Confirmar">
</form>
{% endblock content %}
这是 模板 中按钮所在的部分:
<form action="{% url 'oficios:borrar' oficio.folio %}" method='POST'>
{% csrf_token %}
<button type="submit" class="btn btn-danger btn-lg" data-toggle="tooltip" data-placement="top" title="Eliminar"><i class="fas fa-trash-alt"></i></button>
</form>
这里是删除views.py中对象的方法
@login_required(login_url="/accounts/login/")
def borrar_oficio(request, pk):
oficio = Oficio.objects.get(folio=pk)
if request.method == "POST":
oficio.delete()
return redirect('oficios:list')
context = {'item': oficio}
return render(request, "borrar_oficio.html", context)
还有我的那个应用的urls.py:
app_name = 'oficios'
urlpatterns = [
path('', views.lista_oficios, name="list"),
path('crear/', views.crear_oficio, name="crear"),
path('editar_oficio/<str:pk>', views.editar_oficio, name="editar"),
path('borrar_oficio/<str:pk>', views.borrar_oficio, name="borrar"),
path('dependencia/', views.agregar_dependencia, name="dependencia"),
]
【问题讨论】:
标签: django django-rest-framework django-views django-templates