2017-07-14 13 views
0

私はトルネードではかなり新しいです。以下のようなものを使うことはできますか?竜巻:ルートの同じクラスの異なるメソッド

クラス

class HomeHandler(BaseHandler): 

    def get(self): 
     return self.render("home.html") 

    def login(self): 
     return self.render("login.html") 

ルート

(r"/", HomeHandler), 
(r"/login", HomeHandler.login, dict(db=db)), 

これは動作しません。私はHomeHandler.login()を使用しようとしましたが、必要な参照を渡す方法がわかりません(自己に似ているはずです)。

ご協力いただきありがとうございます。ありがとう

答えて

1

トルネードは、「ハンドラ」という概念を使用します。これは、特定のパスで要求を処理します。ハンドラはクラスです。内部的には、リクエストで使用されるHTTP動詞に対応するこれらのクラスからメソッドを選択します。あなたのケースでは

、あなたは、2つのパスがあります。//loginを、のは、「ホーム」と、それぞれ「ログインと呼んでみましょうさて、あなたは2つのハンドラ持っている必要があります:HomeHandlerLoginHandlerを、対応するルートに割り当てます...

ルート:

(r"/", HomeHandler), 
(r"/login", LoginHandler, {"db": db}) 

ハンドラクラス:

class HomeHandler(BaseHandler): 

    def get(self): 
     # Will work for GET yoursite.com/, e.g. when opened in a browser 
     # The next line will render a template and return it to the browser 
     self.render("home.html") 


class LoginHandler(BaseHandler): 

    def initialize(self, db): 
     # That `db` from route declaration is passed as an argument 
     # to this Tornado specific method 
     self.db = db 

    def get(self): 
     # Will work for GET yoursite.com/login, e.g. when opened in a browser 
     # You may use self.db here 
     # The next line will render a template and return it to the browser 
     self.render("login.html") 

    def post(self): 
     # Will work for POST yoursite.com/login, e.g. when the data 
     # from the form on the Login page is sent back to the server 
     # You may use self.db here 
     return 
1

いいえ、それはできません。 Tornadoは、HTTPリクエスト(getpostなど)に基づいてどのメソッドを呼び出すかを選択するため、ルーティングテーブルに代替メソッドを指定することはできません。代わりに別のクラスを使用します(おそらく、共通の基本クラスを使用します)。

関連する問題