2016-03-04 98 views
-3

我無法從另一個類別調用方法。我得到錯誤,因爲請求沒有被定義,而調用方法。根據討論,我試圖從另一個類像get_user(request)調用方法。並試圖從另一個類訪問變量從另一個類別調用變量的方法問題

class A(object): 
    def get_user(self,request): 
     a = self.request.get_full_path() 

class B(A, AllowEmptyMixin, AutoModelSelect2Field): 
    b = A() 
    b.get_user(request) ---->>> Getting error "request" is not defined 

任何人都清楚我的問題

+0

你期望傳遞給'get_user()'什麼? – zondo

+5

@PythonTeam看到你的[python上的其他問題]後(http://stackoverflow.com/questions/35792644/cant-able-to-get-current-url-in-python)。我的意思是沒有不尊重,但你可能想考慮檢查python教程的基礎知識。你正在編寫的代碼並沒有形成太多的邏輯意義。 – castis

+0

爲什麼你會再次提出[同樣的問題](http://stackoverflow.com/questions/35792644/cant-able-to-get-current-url-in-python)? – AKS

回答

2

當你調用b.get_user(request)你打電話get_user方法,並通過一個名爲request存在變數,但你不具備的要求'在當前範圍內變量。

我不確定你的代碼現在應該做什麼。

+1

,尤其是當他們最終將請求從「self」中抽出時。 – castis

+0

我想獲得當前在另一個類方法中的url路徑。 –

0

更改方法get_user使用方法參數request而不是不存在的self.request變量。

class A(object): 
     def get_user(self,request): 
      a = request.get_full_path() 
0

有兩個地方代碼中斷。

b.get_user(request) ---->>> Getting error "request" is not defined 

在此範圍內沒有請求變量。

並在get_user()中。函數中沒有請求參數。 SO self.request不起作用。