以下适用于Flask-Classful(它是现已废弃的Flask-Classy的维护叉),但我想知道是否有一个" native" Flask-Classful中最后两条路线的逻辑版本,没有混合Flask路线和Flask-Classful内置特殊方法,如get()
和post()
?
从Flask-Classy在https://pythonhosted.org/Flask-Classy/的大量文档中我遗憾地看不到下面代码中最后两条路线的例子,例如。 GET /news/123/comments/
和GET /news/123/comments/321
。有一些例子可能类似于GET /news/comments/123
,但是当您从路径URI中选择两个变量(将用于数据库查询)时则不会这样。
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中有内置的支持吗?
这是一些测试请求,它起作用:
$ 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
答案 0 :(得分:1)
默认情况下,使用某个名称
创建方法时class NewsView(FlaskView):
def comments(self, news_id, comment_id):
pass
默认情况下,上面会将其注册为/comments/<news_id>/<comment_id>
,因为您的基本路线为news/
。评论方法的最终路线将变为news/comments/<news_id>/<comment_id>
。
然后是谁阻止我们改变它?
from flask import Flask, request, jsonify
import json
from flask_classy import FlaskView
app = Flask(__name__)
class MyFlaskView(FlaskView):
@classmethod
def build_rule(cls, rule, method=None):
path = super(MyFlaskView, cls).build_rule(rule, method)
parts = path.split("/")
if len(parts) >= 3 and parts[-1].startswith("<") and parts[-2].startswith("<"):
parts[-3], parts[-2] = parts[-2], parts[-3]
return "/".join(parts)
return path
class NewsView(MyFlaskView):
def comments(self, news_id, comment_id):
return "This is GET /news\n"
def get(self, news_id):
return "This is GET /news2\n"
if __name__ == "__main__":
NewsView.register(app, route_base="news/")
app.run(debug=True)
此重写方法会将您的路线注册为/news/<news_id>/comments/<comment_id>