Я получаю Reverse for 'employee-delete' with arguments '('',)' not found. 1 pattern(s) tried: ['employee/employee\\-list/(?P<id>[0-9]+)/delete/$']
эту ошибку, когда пытаюсь удалить сотрудника. Я не знаю, почему возникает эта ошибка. Я указал правильный URL-адрес. Я хочу удалить сотрудника из моей таблицы списка, используя ajax и jquery.
employee_list. html:
{% extends "base.html" %}
{% block content %}
{% load static %}
<link rel="stylesheet" href="{% static 'employee/css/master.css' %}">
<div class="">
<div class="table-wrapper">
<div class="table-title">
<div class="row">
<div class="col-sm-6">
<h2><b>Employees</b></h2>
</div>
<div class="col-sm-6">
<a href="{% url 'employee:employee-add' %}" data-target="exampleModal" class="btn btn-success" data-toggle="modal">
<span ></span>
<i class="material-icons"></i>
<span data-feather="plus"></span>Add New Employee
</a>
<!--<a href="#deleteEmployeeModal" class="btn btn-danger" data-toggle="modal"><i class="material-icons"></i> <span>Delete</span></a>-->
</div>
</div>
</div>
<table class="table table-striped table-hover">
<thead>
<tr>
<th>
<span class="custom-checkbox">
<input type="checkbox" id="selectAll">
<label for="selectAll"></label>
</span>
</th>
<th>ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
<th>Address</th>
<th>Phone</th>
<th>Department</th>
<th>Designation</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<!-- Loop for showing employee list in a table-->
{% for employee in employees %}
<tr>
<td>
<span class="custom-checkbox">
<input type="checkbox" id="checkbox1" name="options[]" value="1">
<label for="checkbox1"></label>
</span>
</td>
<td>{{employee.e_id}}</td>
<td>{{employee.first_name}}</td>
<td>{{employee.last_name}}</td>
<td>{{employee.email}}</td>
<td>{{employee.address}}</td>
<td>{{employee.phone_number}}</td>
<td>{{employee.department}}</td>
<td>{{employee.designation}}</td>
<td>
<a href="{% url 'employee:employee-update' employee.id %}" class="edit" data-toggle="modal">
<i class="material-icons" data-toggle="tooltip" title="Edit"></i>
<span data-feather="edit-2"></span>
</a>
<a href="{% url 'employee:employee-delete' employee.id %}" id="btn" date-target="#deleteEmployeeModal" onclick="return confirm('Are you sure?');" class="delete" data-toggle="modal">
<i class="material-icons" data-toggle="tooltip" title="Delete"></i>
<span data-feather="trash"></span>
</a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
<div class="">
{% if is_paginated %}
<ul class="pagination">
{% if page_obj.has_previous %}
<li><a href="?page={{ page_obj.previous_page_number }}"><span data-feather="chevron-left"></span></a></li>
{% else %}
<li class="disabled"><span data-feather="chevron-left"></span></li>
{% endif %}
{% for i in paginator.page_range %}
{% if page_obj.number == i %}
<li class="active"><span class="pagination-number pagination-current">{{ i }} <span class="sr-only">(current)</span></span></li>
{% else %}
<li><a class="pagination-number" href="?page={{ i }}">{{ i }}</a></li>
{% endif %}
{% endfor %}
{% if page_obj.has_next %}
<li><a href="?page={{ page_obj.next_page_number }}"><span data-feather="chevron-right"></span></a></li>
{% else %}
<li class="disabled"><span data-feather="chevron-right"></span></li>
{% endif %}
</ul>
{% endif %}
</div>
</div>
<div id="deleteEmployeeModal" class="modal fade">
<div class="modal-dialog">
<div class="modal-content"
</div>
</div>
</div>
{% endblock %}
{% block scripts %}
{{block.super}}
<script type="text/javascript">
$.ajax("{% url 'employee:employee-delete' employee.id %}", {
method: 'POST',
success: function() {
window.location.reload();
},
});
</script>
<script type="text/javascript">
// Select/Deselect checkboxes
var checkbox = $('table tbody input[type="checkbox"]');
$("#selectAll").click(function(){
if(this.checked){
checkbox.each(function(){
this.checked = true;
});
} else{
checkbox.each(function(){
this.checked = false;
});
}
});
checkbox.click(function(){
if(!this.checked){
$("#selectAll").prop("checked", false);
}
});
</script>
{% endblock scripts %}
Я добавил скрипт, указывающий на url 'employee: employee- delete 'employee.id% это файл urls.py
urls.py:
from django.urls import path
from django.urls import reverse
from . views import (
EmployeeListView,
EmployeeAddView,
EmployeeDetailView,
EmployeeUpdateView,
EmployeeDeleteView,
)
app_name = 'employee'
urlpatterns = [
path('employee-list/', EmployeeListView.as_view(), name='employee-list'),
path('employee-add/', EmployeeAddView.as_view(), name='employee-add'),
path('employee-list/<int:id>/', EmployeeDetailView.as_view(), name='employee-detail'),
path('employee-list/<int:id>/update/', EmployeeUpdateView.as_view(), name='employee-update'),
path('employee-list/<int:id>/delete/', EmployeeDeleteView.as_view(), name='employee-delete'),
]
views.py:
class EmployeeDeleteView(DeleteView):
"""
Deletes a created employee
"""
template_name = 'employee/employee_list.html'
model = Employee
def get_object(self):
id_ = self.kwargs.get("id")
return get_object_or_404(Employee, id=id_)
def get(self, request, *args, **kwargs):
employee = self.get_object()
return render(request, 'employee/employee_delete.html', {'employee': employee})
def get_success_url(self):
return reverse('employee:employee-list')