Home > Mobile >  Django : FieldError at /editstu/121
Django : FieldError at /editstu/121

Time:11-12

Error :

Cannot resolve keyword 'id' into field. Choices are: Age, Course_ID, DoB, Grade, Student_ID, Student_Name

enter image description here

My function in views.py

def Editstu(request,id):
    editstuobj = Student.objects.get(id=id)
    return render(request, 'editstu.html',{'Student':editstuobj})

My urls.py

urlpatterns = [
    path("admin/", admin.site.urls),
    path('',views.showStudent),
    path('InsertStudent',views.InsertStudent,name="InsertStudent"),
    path('Insertcour',views.InsertCourse,name="InsertCourse"),
    path('editstu/<int:id>',views.Editstu,name="Editstu"),
    path('editcour/<int:id>',views.Editcour,name="Editcour"),
    # path('Update/<int:id>',views.updateemp,name="updateemp"),
    path('Deletestu/<int:id>',views.Deletestu,name="Deletestu"),
    path('Deletecourse/<int:id>',views.Deletecourse,name="Deletecourse"),
]

My Index.html

<td><a href="editstu/{{result.Student_ID}}">Edit</a></td>
<td><a href="Deletestu/{{result.Student_ID}}" onclick="return confirm ('Are you sure to delete the record?')">Delete</a></td>
         

I checked the function if it is declared properly but no luck. I'm new to Django. It says error is 'id' but it should be fine. Please help if anyone can.

CodePudding user response:

I think the model field is Student_ID not id and also use get_object_or_404() so:

def Editstu(request,id):
    editstuobj = get_object_or_404(Student,Student_ID=id)
    return render(request, 'editstu.html',{'Student':editstuobj})

Also use url tags so:

<td><a href="{% url 'Editstu' result.Student_ID %}">Edit</a></td>
<td><a href="{% 'Deletestu' result.Student_ID %}" onclick="return confirm ('Are you sure to delete the record?')">Delete</a></td>
  • Related