Reputation: 181
I am working on a project in django. I'm trying to do delete Confirmation alert before deleting data. How to do.
Any advice would be much appreciated.
views.py
def del_data(request, obj_id):
del_data = Order_data.objects.filter(order_no=obj_id)
if len(del_data) > 0:
del_data.delete()
return redirect('/home/')
return render(request, 'delete_conf.html', {})
urls.py
urlpatterns = [
path('delete/<int:obj_id>/',views.del_data,name="del_data")
]
delete_conf.html
<table id="order_view_table">
<tr>
<th>Name</th>
<th>Email</th>
<th>ACTION </th>
{% for i in result %}
<tr id="get_data">
<td id="name">{{ i.name }} </td>
<td>{{ i.email}} </td>
<td><a href="{% url 'app:edit_data' i.order_no %}" > <i class='fas fa-edit' ></i></a><a href="{% url 'app:del_data' i.order_no %}"><i class='fas fa-trash-alt' ></i></a>
</tr>
{% endfor %}
</table>
Upvotes: 1
Views: 1554
Reputation: 77892
First and most important point: You NEVER change (create / update / delete) your database on a GET request. A GET request MUST be idempotent. If you don't understand why, go browse thedailywtf archives for the story of the database that was emptied each time the google bot crawled the site...
And actually, this is also part of the answer. The canonical solution here is to use the GET request to display a confirmation form (which will use the POST method), and the POST request to effectively delete the data.
If you want to make things more sexy, you can then use ajax + a modal on top of this.
Upvotes: 1
Reputation: 3103
You can use javascript
like this:
<table id="order_view_table">
<tr>
<th>Name</th>
<th>Email</th>
<th>ACTION </th>
{% for i in result %}
<tr id="get_data">
<td id="name">{{ i.name }} </td>
<td>{{ i.email}} </td>
<td><a href="{% url 'app:edit_data' i.order_no %}" > <i class='fas fa-edit' ></i></a><a href="#" onclick="confirm_delete('{{i.order_no}}');"><i class='fas fa-trash-alt' ></i></a>
</tr>
{% endfor %}
</table>
<script>
function confirm_delete(orderno){
var r = confirm("Sure you want to delete?");
if (r == true) {
window.location.href="{% url 'app:del_data' "+ orderno +"%}"
}
</script>
Upvotes: 0