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]> 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/8104CF82-3E9B-4426-8B48-2BE627C805D6%40gmail.com.
For more options, visit https://groups.google.com/d/optout.