UnboundLocalError

31 views
Skip to first unread message

Sharanya Purushothaman Nair

unread,
Nov 10, 2016, 11:06:18 AM11/10/16
to Django users
Hi Everyone, 

Class FilterSearch(View):
    template_name = 'approved/approvedElementsSEView.html

    def post(self, request, testPlanId):
        elemType = request.POST.get('testElementType);
        elemCategory = request.POST.get('category');
        
        if(elemCategory=='routing'):

            testElement = ApprovedTestElement.objects.filter(testElemType=elemType,routing='Y');

        elif(elemCategory=='switching'):

            testElement = ApprovedTestElement.objects.filter(testElemType=elemType,switching='Y');
         
            return render(request,self.template_name,{'testElement':testElement,'testPlanId':testPlanId})

I am trying to send a different testElement based on which if condition has been satisfied. I am new to Django and I keep getting UnboundLocalError Exception : local variable 'testElement' referenced before assignment . I have tried defining testElement outside the if loop, but maybe that's not the right way! Any Help will be greatly appreciated! 
            






GMail

unread,
Nov 10, 2016, 11:10:06 AM11/10/16
to django...@googlegroups.com
Hi!

That's about Python. You can't use variable if it wasn't defined earlier.
In your code return statement should have one less indentation level:

...
if(elemCategory=='routing'):
    testElement = ApprovedTestElement.objects.filter(testElemType=elemType,routing='Y');
elif(elemCategory=='switching'):
    testElement = ApprovedTestElement.objects.filter(testElemType=elemType,switching='Y');
 
return render(request,self.template_name,{'testElement':testElement,'testPlanId':testPlanId})


Also, you should definitely read PEP-8 - https://www.python.org/dev/peps/pep-0008/.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/ac684cc5-6d93-497e-b93e-f7b486b0efa8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

GMail

unread,
Nov 10, 2016, 11:19:40 AM11/10/16
to django...@googlegroups.com
Also, you're missing 'else' statement, so if elemCategory is neither 'routing' nor 'switching', testElement will never be defined.
You should either assign default value to testElement before if-else block or add 'else' statement, like so:

testElement = ApprovedTestElement.objects.filter(testElemType=elemType)  # note that .filter() returnes QuerySet, not a single object
if ... :
    ...
elif ... :
    ...

return render(..., {'testElement': testElement})


or


if ... :
    ...
elif ... :
    ...
else:
    testElement = ApprovedTestElement.objects.filter(testElemType=elemType)

return render(..., {'testElement': testElement})
Reply all
Reply to author
Forward
0 new messages