Issue
When I'm deleting from the Patient table I'm getting this error:
DoesNotExist at /delete/4/
Employee matching query does not exist.
views.py
def delete_emp(request, pk):
if request.method == 'POST':
pi = Employee.objects.get(id=pk)
pi.delete()
return redirect('/employee')
def delete_pat(request, pk):
if request.method == 'POST':
pi = Patient.objects.get(id=pk)
pi.delete()
return redirect('/patient')
this is html page:
<td>
<form action="{% url 'home:delete_pat' p.id %}" method="post">
{% csrf_token %}
<input type="submit" class="btn btn-primary btn-sm" value="Delete">
</form>
</td>
models.py
class Patient(models.Model):
name = models.CharField(max_length=200)
phone=models.CharField(max_length=17,default='')
age=models.IntegerField(default='')
gender = models.CharField(max_length=20)
doctor_name = models.CharField(max_length=200)
def __str__(self):
return self.name
urls.py
urlpatterns = [
path('', HomeView.as_view(), name='homes'),
path('add/<int:pk>/', addqty, name='add'),
path('edit/<int:pk>/',EditProdView.as_view(), name='edit'),
path('supedit/<int:pk>/',EditSupplierView.as_view(), name='supedit'),
path('createemployee/', views.CreateEmployeeView, name='create-employee'),
path('createpatient/', views.CreatePatientView, name='create-patient'),
path('empedit/<int:pk>/',EditEmployeeView.as_view(), name='empedit'),
path('delete/<int:pk>/', views.delete_emp, name='delete_emp'),
path('patedit/<int:pk>/',EditPatientView.as_view(), name='patedit'),
path('delete/<int:pk>/', views.delete_pat, name='delete_pat'),
path('employee/', views.employees, name='employee-list'),
path('patient/', views.patients, name='patient-list'),
]
Please, can anyone help me to get out of this, any help would be greatly appreciated?
Solution
use filter, so it doesn't throw an error like matching query does not exist
example:
def delete_emp(request, pk):
if request.method == 'POST':
pi = Employee.objects.filter(id=pk).fisrt()
if pi:
pi.delete()
return redirect('/employee')
Answered By - Kiran Chauhan
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.