2017-05-14 14 views
1

我想在Django 1.10項目中使用我自己的第一個中間件。目前運行到以下錯誤嘗試在Django中定義中間件時丟失位置參數錯誤

TypeError: init() missing 1 required positional argument: 'get_response'

我已經在middleware.py定義的中間件是這樣的:

從zeon.utils導入RequestLogThread

class StartRestEndPointMiddleWare(object): 
    def __init__(self, get_response): 
     self.get_response = get_response 
     # One-time configuration and initialization. 

    def __call__(self, request): 
     # Code to be executed for each request before 
     # the view (and later middleware) are called. 
     request.request_log_id = RequestLogThread('send_contact', request.data).start() 

     response = self.get_response(request) 

     # Code to be executed for each request/response after 
     # the view is called. 

     return response 

我迷失試圖找出什麼錯了,因爲一切似乎都是按照doc。我真的很感激任何提示。

UPDATE: 我把它放到middle_classes:

MIDDLEWARE_CLASSES = [ 
    'zeon.middleware.StartRestEndPointMiddleWare', 
] 
+0

您是將它放在'MIDDLEWARE'字典還是放在'MIDDLEWARE_CLASSES'字典中? – Blender

+0

我已根據您的問題 –

回答

2

Django的改變了中間件類在1.10工作。新風格的中間件類屬於在MIDDLEWARE列表,像這樣:

MIDDLEWARE = [ 
    'zeon.middleware.StartRestEndPointMiddleWare', 
] 

當你把你的中間件爲MIDDLEWARE_CLASSES,它被視爲一個老式的中間件,它的工作方式不同。更好的錯誤信息可能會使這個更清楚。

+0

更新了問題,其他中間件又如何?我應該將它們放在'MIDDLEWARE_CLASSES'中,並且只將用戶定義的中間件移動到'MIDDLEWARES'?或者我應該把所有東西都放在'MIDDLEWARES'上? –

+0

看起來應該是'MIDDLEWARE'而不是'MIDDLEWARES' –

+0

@EdgarNavasardyan:謝謝,修正。所有內置的中間件都支持這兩種,所以你可以將所有內容都移動到'MIDDLEWARE'。您不能同時擁有「MIDDLEWARE」和「MIDDLEWARE_CLASSES」。 – Blender

相關問題