以下はFlask-Classfulで動作しますが(現在廃止されているFlask-Classyの維持フォークです)、論理Iの「ネイティブ」バージョンがあるかどうかは疑問でしたFlask-Classfulの最後の2つのルートは、FlaskルートとFlask-Classful組み込み特殊メソッド(例えば、get()
とpost()
など)を混ぜる必要はありませんか?Flask-Classy/Flask-Classfulのサブルート
Flask-Classyはhttps://pythonhosted.org/Flask-Classy/にあります残念ながら、私は残念ながら以下のコードの最後の2つのルートの例を見つけることはできませんでした。 GET /news/123/comments/
およびGET /news/123/comments/321
。 GET /news/comments/123
のようなケースがいくつかありましたが、ルートURI(DBクエリに使用される)から2つの変数を選択した場合はそうではありません。
from flask_classful import FlaskView, route
class NewsView(FlaskView):
def index(self):
return "This is GET /news\n"
def get(self, news_id):
return "This is GET /news/{}\n".format(news_id)
def post(self):
return "This is POST /news\n"
def put(self, news_id):
return "This is PUT /news/{}\n".format(news_id)
def patch(self, news_id):
return "This is PATCH /news/{}\n".format(news_id)
def delete(self, news_id):
return "This is DELETE /news/{}\n".format(news_id)
@route("/<int:news_id>/comments/<int:comment_id>", methods=["GET"])
def news_comment(self, news_id, comment_id):
return "This is GET /news/{}/comments/{}\n".format(news_id, comment_id)
@route("/<int:news_id>/comments/", methods=["GET"])
def news_comments(self, news_id):
return "This is GET /news/{}/comments/\n".format(news_id)
ルートが登録されている:
def register_views(app):
api_path = "/api/1.0"
from apps.news.views import NewsView
NewsView.register(app, route_base="{}/news/".format(api_path))
が動作しているようです。たぶん髪の毛を分割しているだけかもしれませんが、このようなものはFlask-Classfulのサポートが組み込まれていると思いますか?あなたには、いくつかの名前
class NewsView(FlaskView):
def comments(self, news_id, comment_id):
pass
/comments/<news_id>/<comment_id>
として、あなたのベース以来、それを登録しますデフォルトでは、上記を使用してメソッドを作成する場合、デフォルトで
$ curl -X GET https://localhost:443/api/1.0/news/ --insecure -L
This is GET /news
$ curl -X GET https://localhost:443/api/1.0/news/123 --insecure -L
This is GET /news/123
$ curl -X POST https://localhost:443/api/1.0/news/ --insecure -L
This is POST /news
$ curl -X PUT https://localhost:443/api/1.0/news/123 --insecure -L
This is PUT /news/123
$ curl -X PATCH https://localhost:443/api/1.0/news/123 --insecure -L
This is PATCH /news/123
$ curl -X DELETE https://localhost:443/api/1.0/news/123 --insecure -L
This is DELETE /news/123
$ curl -X GET https://localhost:443/api/1.0/news/1/comments/ --insecure -L
This is GET /news/1/comments/
$ curl -X GET https://localhost:443/api/1.0/news/1/comments/2 --insecure -L
This is GET /news/1/comments/2
ですから、 'news_comment'を使用したい
/news/<news_id>/comments/<comment_id>
として、あなたのルートを登録しますか?それはあなたの質問ですか? –@ TarunLalwaniはい。基本的に私は複数のサブパス(それらはすべて ''/api/1.0/news/''のパスを共有しています)を '' get() ''を同じビューで使うFlask-Classyの方法を探しています。 –