2016-11-10 15 views
0

我是Django的新手,我試圖根據if語句滿足而發送不同的上下文變量。這是我的看法:Django:NameError at/approved/filter/7/- name'testElement'未定義

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:名字「testElement ' 沒有定義。任何幫助將不勝感激!

+0

如果沒有'if' /'elif'是真的會發生什麼?順便說一下,行尾的';'不是必需的,並且條件周圍的括號也不是必需的(儘管它們不影響錯誤)。 – cdarke

+1

Global在這裏不是正確的解決方案;你不應該隨意使用它來解決名稱錯誤。你爲什麼要在if和elif裏面以及在方法結尾返回? –

+0

謝謝!我做了上述修改並刪除了elif,現在它可以工作。 –

回答

1

類FilterSearch(查看): TEMPLATE_NAME = '批准/ 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}) 
+0

上面的代碼有效! –

0

You'l需要處理的幾個案例。

案例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}) 
+0

謝謝!會這樣做:) –