Django:NameError at / approved / filter / 7 / - name' testElement'未定义

时间:2016-11-10 14:54:11

标签: python django django-views

我是Django的新手,我正在尝试发送一个不同的上下文变量,具体取决于if语句是否满足。这是我的观点:

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'):
            global testElement;
            testElement=ApprovedTestElement.objects.filter(testElementType=elemType, routing='y');
            return testElement
        elif(elemCategory=='switching'):
            global testElement;
            testElement = ApprovedTestElement.objects.filter(testElementType=elemType, switching='y');
            return testElement


        return render(request,self.template_name,{'testElement':testElement,'testPlanId':testPlanId})

我最初得到一个UnboundLocalError:局部变量' testElement'在赋值之前引用,我试着通过将testElement定义为全局变量来修复,现在我得到一个NameError:name' testElement'没有定义。任何帮助将不胜感激!

2 个答案:

答案 0 :(得分:1)

类FilterSearch(查看):     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(testElementType=elemType, routing='y');
    if(elemCategory=='switching'):
        testElement = ApprovedTestElement.objects.filter(testElementType=elemType, switching='y');   


    return render(request,self.template_name,{'testElement':testElement,'testPlanId':testPlanId})

答案 1 :(得分:0)

你需要处理更多的案件。

案例1: - 如果elemCategory未定义怎么办?

案例2: - 如果elemCategory不是“路由”或“切换”怎么办?

也许这会有所帮助。

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

    if elemCategory:
        if(elemCategory=='routing'):
            testElement = ApprovedTestElement.objects.filter(testElementType=elemType, routing='y')
        if(elemCategory=='switching'):
            testElement = ApprovedTestElement.objects.filter(testElementType=elemType, switching='y')
        if not (elemCategory=='routing' or elemCategory=='switching'):
            testElement = 'Not Found!' #You can change this to your requirement

    else:
        testElement = 'Not Found!' #You can change this to your requirement

    return render(request,self.template_name,{'testElement':testElement,'testPlanId':testPlanId})

或者

def post(self,request,testPlanId):
    elemType = request.POST.get('testElementType')
    elemCategory = request.POST.get('category')
    testElement = 'Not Found!' #You can change this to your requirement

    if elemCategory:
        if(elemCategory=='routing'):
            testElement = ApprovedTestElement.objects.filter(testElementType=elemType, routing='y')
        if(elemCategory=='switching'):
            testElement = ApprovedTestElement.objects.filter(testElementType=elemType, switching='y')

    return render(request,self.template_name,{'testElement':testElement,'testPlanId':testPlanId})