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})


> On 10 Nov 2016, at 19:09, GMail <[email protected]> wrote:
> 
> 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/ 
> <https://www.python.org/dev/peps/pep-0008/>.
> 
>> On 10 Nov 2016, at 17:22, Sharanya Purushothaman Nair 
>> <[email protected] <mailto:[email protected]>> wrote:
>> 
>> 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! 
>>             
>> 
>> 
>> 
>> 
>> 
>> 
>> 
>> -- 
>> 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 [email protected] 
>> <mailto:[email protected]>.
>> To post to this group, send email to [email protected] 
>> <mailto:[email protected]>.
>> Visit this group at https://groups.google.com/group/django-users 
>> <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
>>  
>> <https://groups.google.com/d/msgid/django-users/ac684cc5-6d93-497e-b93e-f7b486b0efa8%40googlegroups.com?utm_medium=email&utm_source=footer>.
>> For more options, visit https://groups.google.com/d/optout 
>> <https://groups.google.com/d/optout>.
> 

-- 
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 [email protected].
To post to this group, send email to [email protected].
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/F2A1CC0C-8E02-409A-BD98-139286854025%40gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to