2
votes

I encounter this error for my django project. my app is called "scoresubmission" basially i have a feature in the website to allow user download report. So in my views.py file i have report function and import report.py file, where it shows how report is built
It shows the error happens in this line of code:

 submission=Submission.objects.get(month=month,year=reportyear,program=program)

Views.py

def report(request):    

    from scoresubmission.report import reportA, reportB, reportC
    reportType = request.POST["reportType"]
    reportYear = int(request.POST["reportYear"])

    if reportType == 'a':
        report_content = reportA(reportYear)
        response = HttpResponse(report_content, content_type="text/csv")
        response['Content-Disposition'] = 'inline; filename=5SAuditYearlySummaryReport_%d.xlsx' %reportYear

report.py where it has the relevant code for facility in facilities: worksheet.write(row,col,facility.name,facility_format)

    for i in range(12): # 12 months
        month=i+1
        programs=Program.objects.filter(facility_id=facility.id)
        avg_totalscore=0
        count=1
        for program in programs:
            print(program)
            try:

                submission=Submission.objects.get(month=month,year=reportyear,program=program)
                print(submission)
                avg_score=Result.objects.filter(submission=submission).aggregate(Avg('NewScore'))
                #print avg_score.get('NewScore__avg')
                avg_totalscore=(avg_totalscore + avg_score.get('NewScore__avg'))/count
                count=count+1
            except submission.DoesNotExist:
                pass

                #print avg_totalscore
        if avg_totalscore!=0:
            worksheet.write(row,i+3,avg_totalscore,red_format)
        else:
            worksheet.write(row,i+3,'-',red_format)

Traceback (most recent call last): File "C:\Users\CHLOZHAO\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\handlers\exception.py", line 34, in inner response = get_response(request) File "C:\Users\CHLOZHAO\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response response = self.process_exception_by_middleware(e, request) File "C:\Users\CHLOZHAO\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response response = wrapped_callback(request, *callback_args, **callback_kwargs) File "C:\Users\CHLOZHAO\AppData\Local\Programs\Python\Python37\lib\site-packages\django\contrib\auth\decorators.py", line 21, in _wrapped_view return view_func(request, *args, **kwargs) File "C:\D Drive\5S Audit Website\my5saudit\scoresubmission\views.py", line 185, in report report_content = reportA(reportYear) File "C:\D Drive\5S Audit Website\my5saudit\scoresubmission\report.py", line 79, in reportA except submission.DoesNotExist: UnboundLocalError: local variable 'submission' referenced before assignment

1

1 Answers

0
votes

In your except you need to refer to the class Submission, not the object, since it does not per se exists at that time:

try:
    submission=Submission.objects.get(month=month,year=reportyear,program=program)
    print(submission)
    avg_score=Result.objects.filter(submission=submission).aggregate(Avg('NewScore'))
    #print avg_score.get('NewScore__avg')
    avg_totalscore=(avg_totalscore + avg_score.get('NewScore__avg'))/count
    count=count+1
except Submission.DoesNotExist:  # reference to the class, not the object
    pass

If Sibmission.objects.get(..) fails, then the submission variable is never assigned, and hence submission.DoesNotExist makes no sense.

You acutally should never use the object, and always use the model class itself to refer to the DoesNotExist exception class.