DoesNotExist at /up_emp/31/
Employee matching query does not exist.Request Method:POSTRequest URL:http://127.0.0.1:8000/up_emp/31/Django Version:5.0.6Exception Type:DoesNotExistException Value:Employee matching query does not exist.While I was implementing CRUD operations in Django, I encountered some errors during the update process. Could you please analyze the code provided below and guide me on how to resolve these errors?
update.html
<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1"><title>Add employee</title><link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet"><style> body { background-color: #222021; font-family: sans-serif; margin: 0; padding: 0; height: 100vh; display: flex; flex-direction: column; justify-content: center; align-items: center; } .card { border-radius: 10px; box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1); } .form-control { border-radius: 5px; box-shadow: none; border: 1px solid #ccc; } .btn-primary { background-color: #3498db; border-color: #3498db; } .btn-primary:hover { background-color: Blue; } .btn-back-home { color: red; background-color: yellow; border: none; border-radius: 5px; padding: 10px 20px; text-decoration: none; } .btn-back-home:hover { background-color: yellow; text-decoration: none; } #A,#B,#C,#D,#E,#F,#G,#H,#I { font-weight: bold; }</style></head><body><div class="container"><div class="row justify-content-center align-items-center"><div class="col-sm-8 col-md-6 col-lg-5 card shadow-lg px-4 py-5"><h2 class="text-center mb-4">Update Employee</h2><form action="/up_emp/{{emp.id}}/" method="POST"> {% csrf_token %}<div class="form-group" id="A"><label for="fname">Full name</label><input type="text" class="form-control" id="fname" name="fname" placeholder="Enter first name" value="{{emp.name}}"></div><div class="form-group" id="B"><label for="eid">Employee Id </label><input type="text" class="form-control" id="eid" name="eid" placeholder="Enter your employee id" value="{{emp.eId}}"></div><div class="form-group" id="C"><label for="phn">Phone number </label><input type="text" class="form-control" id="phn" name="phn" placeholder="98xxxxxxxx" value="emp.phone"></div><div class="form-group" id="D"><label for="Address"> Address</label><input type="text" class="form-control" id="Address" name="address" placeholder="Enter your address" value="{{emp.address}}"></div><br><div class="form-group"><label for="work" id="F"> Working</label><input type="checkbox" class="form-check-input" id="work" name="work" value="{{emp.address}}" ></div><br><div class="form-group"><label id="E">Select gender</label><div class="form-check"><label class="form-check-label" for="male"> Male </label><input class="form-check-input" type="radio" name="gender" id="male" value="Male" {% if emp.gender == 'Male' %} checked {% endif %}></div><div class="form-check"><label class="form-check-label" for="female"> Female </label><input class="form-check-input" type="radio" name="gender" id="female" value="Female" {% if emp.gender == 'Female' %} checked {% endif %}></div><div class="form-check"><label class="form-check-label" for="other"> Other </label><input class="form-check-input" type="radio" name="gender" id="other" value="Other" {% if emp.gender == 'Other' %} checked {% endif %}></div></div><div class="row justify-content-between"><button type="submit" class="btn btn-danger btn-sm"> Update</button> </div></form><br></div></div></div><script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script><script src="https://cdn.jsdelivr.net/npm/popper.js@1.12.9/dist/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script><script src="https://cdn.jsdelivr.net/npm/bootstrap@4.0.0/dist/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script></body></html>views.py
def update_emp(request,emp_id): emp=Employee.objects.get(pk=emp_id) return render(request,'update_emp.html',{'emp':emp})def up_emp(request,emp_id=None): if request.method=="POST": emp_name=request.POST.get("fname") emp_id=request.POST.get("eid") emp_phn=request.POST.get("phn") emp_address=request.POST.get("address") emp_working=request.POST.get("work") emp_gender=request.POST.get("gender") emp = Employee.objects.get(pk=emp_id) emp.name=emp_name emp.eId=emp_id emp.phone=emp_phn emp.address=emp_address emp.working=emp_working emp.gender=emp_gender if emp_working is None: emp.working=False else: emp.working=True emp.save() return redirect("/index/") urls.py
from django.contrib import adminfrom django.urls import pathfrom . import viewsurlpatterns=[ path('admin/', admin.site.urls), path('',views.home), path('index/',views.home), path('add_emp/',views.add_emp), path('delete_emp/<int:emp_id>/', views.delete_emp), path('delete_emp/', views.delete_emp), path("update_emp/<int:emp_id>/",views.update_emp), path("up_emp/<int:emp_id>/",views.up_emp), path('displayAll_emp/', views.displayAll_emp),]models.py
from django.db import models# Create your models here.class Employee(models.Model): name=models.CharField(max_length=200) eId=models.CharField(max_length=200) phone=models.CharField(max_length=20) address=models.CharField(max_length=200) gender=models.CharField(max_length=20) working=models.BooleanField(default=True)Could you analyze the error given above and provide a valid solution that can resolve these issues? Also, could you explain why this problem is occurring and what might be wrong with my code?