简体   繁体   English

Django Web 应用程序中的 UnboundLocalError

[英]UnboundLocalError in Django web application

I have been trying to debug this after some few days with no solutions yet, I would be glad if I could get a solution or suggestion.几天后我一直在尝试调试它,但还没有解决方案,如果我能得到解决方案或建议,我会很高兴。 Thanks谢谢

I get the UnboundLocalError, things were working perfectly but when I made some changes to my models.py to fix some other bug which got resolved, this came about.我得到了 UnboundLocalError,一切正常,但是当我对我的 models.py 进行一些更改以修复其他一些已解决的错误时,出现了这种情况。

ERROR LOGS

Traceback (most recent call last):
  File "C:\Users\Habib\Documents\django\django-new\student-management-system\venv\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
    response = get_response(request)
  File "C:\Users\Habib\Documents\django\django-new\student-management-system\venv\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "C:\Users\Habib\Documents\django\django-new\student-management-system\venv\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\Habib\Documents\django\django-new\student-management-system\student_management_app\StaffViews.py", line 31, in staff_home
    students_count = student.objects.filter(course_id__in=final_course).count()

Exception Type: UnboundLocalError at /staff_home/
Exception Value: local variable 'student' referenced before assignment
View.py
def staff_home(request):
    # Fetching All Students under Staff
    subjects = Subjects.objects.filter(staff_id=request.user.id)
    course_id_list = []
    for subject in subjects:
        course = Courses.objects.get(id=subject.course_id.id)
        course_id_list.append(course.id)
    final_course = []
    # Removing Duplicate Course Id
    for course_id in course_id_list:
        if course_id not in final_course:
            final_course.append(course_id)
    
    students_count = student.objects.filter(course_id__in=final_course).count()
    subject_count = subjects.count()
    # Fetch All Attendance Count
    attendance_count = Attendance.objects.filter(subject_id__in=subjects).count()
    # Fetch All Approve Leave
    staff = Staffs.objects.get(admin=request.user.id)
    leave_count = LeaveReportStaff.objects.filter(staff_id=staff.id, leave_status=1).count()
    #Fetch Attendance Data by Subjects
    subject_list = []
    attendance_list = []
    for subject in subjects:
        attendance_count1 = Attendance.objects.filter(subject_id=subject.id).count()
        subject_list.append(subject.subject_name)
        attendance_list.append(attendance_count1)
    students_attendance = student.objects.filter(course_id__in=final_course)
    student_list = []
    student_list_attendance_present = []
    student_list_attendance_absent = []
    for student in students_attendance:
        attendance_present_count = AttendanceReport.objects.filter(status=True, student_id=student.id).count()
        attendance_absent_count = AttendanceReport.objects.filter(status=False, student_id=student.id).count()
        student_list.append(student.admin.first_name+" "+ student.admin.last_name)
        student_list_attendance_present.append(attendance_present_count)
        student_list_attendance_absent.append(attendance_absent_count)
    context={
        "students_count": students_count, "attendance_count": attendance_count, "leave_count": leave_count, 
        "subject_count": subject_count, "subject_list": subject_list, "attendance_list": attendance_list, 
        "student_list": student_list, "attendance_present_list": student_list_attendance_present, 
        "attendance_absent_list": student_list_attendance_absent
    }
    return render(request, "staff_template/staff_home_template.html", context)
Models.py

class student(models.Model):
    id = models.AutoField(primary_key=True)
    admin = models.OneToOneField(CustomUser, on_delete = models.CASCADE)
    gender = models.CharField(max_length=50)
    profile_pic = models.FileField()
    address = models.TextField()
    course_id = models.ForeignKey(Courses, on_delete=models.DO_NOTHING, default=1)
    session_year_id = models.ForeignKey(SessionYearModel, on_delete=models.CASCADE)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
    objects = models.Manager()

    quizzes = models.ManyToManyField(Quiz, through='TakenQuiz')
    interests = models.ManyToManyField(Subjects, related_name='interested_students')

    def get_unanswered_questions(self, quiz):
        answered_questions = self.quiz_answers \
            .filter(answer__question__quiz=quiz) \
            .values_list('answer__question__pk', flat=True)
        questions = quiz.questions.exclude(pk__in=answered_questions).order_by('text')
        return questions

class Attendance(models.Model):
    # Subject Attendance
    id = models.AutoField(primary_key=True)
    subject_id = models.ForeignKey(Subjects, on_delete=models.DO_NOTHING)
    attendance_date = models.DateField()
    session_year_id = models.ForeignKey(SessionYearModel, on_delete=models.CASCADE)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)
    objects = models.Manager()

Assigning a value to a variable inside a function makes that variable local.为函数内的变量赋值会使该变量成为局部变量。 Since the error is with student , the trick is to see if you are assigning a value to something named "student".由于错误出在student ,所以诀窍是查看您是否为名为“student”的东西赋值。 Sure enough果然

for student in students_attendance:

The for loop assigns iterated objects to "student". for循环将迭代对象分配给“student”。 That makes student a local variable, shadowing the student class.这使得student成为局部变量,隐藏了student类。 Just use a different name for that variable, like maybe "student_attending".只需为该变量使用不同的名称,例如“student_attending”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM