下面的Flask-Classful(它是现在被废弃的Flask-Classy的维护叉)的工作,但我想知道是否有逻辑I的“本地”版本在Flask-Classful的最后两条路线中,没有混合烧瓶路线和Flask-Classful内置特殊方法,如get()
和post()
?Flask-Classy/Flask-Classful中的子路由
来自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
所以你要使用'news_comment'你的路线,而不必使用一个装饰?这是你的问题吗? –
@TarunLalwani是的。基本上我正在寻找一个Flask-Classy的方法来在同一个视图中为多个子路径(它们都共享“/ api/1.0/news /”路径)使用get()。 –