mardi 4 mai 2021

CustomUser matching query does not exist. ERROR( DJANGO)

when i save the form in template. The error says:

CustomUser matching query does not exist.

Line number 104: teacher=CustomUser.objects.get(id=teachers_id)

Model.py

class Subjects(models.Model):
    id=models.AutoField(primary_key=True)
    subject_name=models.CharField(max_length=255)
    course_id=models.ForeignKey(Courses,on_delete=models.CASCADE,default=1)
    teachers_id=models.ForeignKey(CustomUser,on_delete=models.CASCADE)
    created_at=models.DateTimeField(auto_now_add=True)
    updated_at=models.DateTimeField(auto_now_add=True)
    objects=models.Manager()

views.py

def add_subject_save(request):
    if request.method!="POST":
        return HttpResponse("<h2>Method Not Allowed</h2>")
    else:
        subject_name=request.POST.get("subject_name")
        course_id=request.POST.get("course")
        course=Courses.objects.get(id=course_id)
        teachers_id=request.POST.get("teacher")
        teacher=CustomUser.objects.get(id=teachers_id)

        try:
            subject=Subjects(subject_name=subject_name,course_id=course,teachers_id=teacher)
            subject.save()
            messages.success(request,"Successfully Added Subject")
            return HttpResponseRedirect("add_subject")
        except:
            messages.error(request,"Failed to Add Subject")
            return HttpResponseRedirec("add_subject")

I dont know how to solve this error.




Aucun commentaire:

Enregistrer un commentaire