Reputation: 759
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.
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()
Upvotes: 0
Views: 403
Reputation: 77347
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". Sure enough
for student in students_attendance:
The for
loop assigns iterated objects to "student". That makes student
a local variable, shadowing the student
class. Just use a different name for that variable, like maybe "student_attending".
Upvotes: 1