简体   繁体   English

关于根据Flask_restx的API对象声明位置路由函数的问题

[英]Regarding the problem of the route function according to the API object declaration location of Flask_restx

Flask restx is a library that develops api functions by dividing them into files in the flask framework and supports swaggerui. Flask restx 是一个在flask框架中通过分文件来开发api函数的库,支持swaggerui。 When serving api and web pages on one flask server, the api part can be divided into files and developed using restx.在一台flask服务器上服务api和网页时,api部分可以拆分成文件,使用restx开发。 However, there is a caution when registering namespace to write flask restx.但是,注册命名空间来编写flask restx 时有一个注意事项。 The difference between the two source codes below is whether the namespace registration part of api and the app.route function part of api, and whether the namespace registration part of api comes before the app.route or later.下面两个源码的区别是api的namespace注册部分和api的app.route函数部分,api的namespace注册部分是在app.route之前还是之后。

from flask import Flask, render_template, url_for
from datetime import datetime
from flask_restx import Api
from test import test


app = Flask(__name__)


@app.route('/')
def index():
    return 'index'


api = Api(
    app,
    doc="/doc/",
    version="0.1",
    title="test",
)


api.add_namespace(test, '/test')

if __name__ == '__main__':
    app.run(debug=True)

from flask import Flask, render_template, url_for
from datetime import datetime
from flask_restx import Api
from test import test


app = Flask(__name__)
api = Api(
    app,
    doc="/doc/",
    version="0.1",
    title="test",
)


api.add_namespace(test, '/test')


@app.route('/')
def index():
    return 'index'


if __name__ == '__main__':
    app.run(debug=True)

First, when executed with the first source code, it is normally recognized when approaching the / and /test path.首先,当用第一个源代码执行时,通常在接近 / 和 /test 路径时识别。

127.0.0.1 - - [27/Sep/2021 15:40:40] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [27/Sep/2021 15:40:40] "GET /test HTTP/1.1" 404 -

However, the second source code recognizes only /test normally and / does not.但是,第二个源代码只能正常识别 /test 而 / 不能识别。

127.0.0.1 - - [27/Sep/2021 15:40:40] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [27/Sep/2021 15:40:40] "GET /test HTTP/1.1" 200 -

It can be seen that the console log and web browser are also displaying 404 errors when approaching / route.可以看到控制台日志和网页浏览器在接近/路由时也显示404错误。 However, in the second source code, not all of the subroutes of / are not possible.但是,在第二个源代码中,并非所有 / 的子路由都是不可能的。

from flask import Flask, render_template, url_for
from datetime import datetime
from flask_restx import Api
from test import test


app = Flask(__name__)
api = Api(
    app,
    doc="/doc/",
    version="0.1",
    title="test",
)


api.add_namespace(test, '/test')


@app.route('/')
def index():
    return 'index'


@app.route('/main')
def main():
    return 'main'


if __name__ == '__main__':
    app.run(debug=True)

In this way, / root is not recognized, but /main recognizes.这样, /root 不识别,但 /main 识别。

127.0.0.1 - - [27/Sep/2021 15:40:35] "GET / HTTP/1.1" 404 -
127.0.0.1 - - [27/Sep/2021 15:40:40] "GET /test HTTP/1.1" 200 -
127.0.0.1 - - [27/Sep/2021 15:40:45] "GET /main HTTP/1.1" 200 -

I don't know why they do this.我不知道他们为什么这样做。

Try leaving the route string empty to see if that works尝试将路由字符串留空以查看是否有效

@app.route('')
def index():
    return 'index'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM