簡體   English   中英

如何將 url_for 與 flask_restx 一起使用?

[英]How to use url_for with flask_restx?

我可以在flask 2.0.1 模板中使用url_for

<!-- flask-template.html -->
<button onclick="window.location.href='{{ url_for( 'testimport') }}';" >Import</button>

隨着

@app.route('/import/images')
def testimport():
  return "ok"

但我不能用flask-restx 0.5.1 中的資源來做到這一點。

api = Api(app, version='1.0',
          title='The Restx API',
          description='An API to initiate basic operations')
ns_detect = api.namespace('detect', path='/', description='Detection')
@ns_detect.route('/detect/recording')
class DetectRecording(Resource):
    def post(self):
        return {"status": "ok"}

誰能解釋一下,如何在上面的 flask-restx 案例中正確使用url_for

<button value="{{ url_for( 'ns_detect.DetectRecording') }}">1</button> <!-- can't build -->
<button value="{{ Api.url_for( 'DetectRecording') }}">2</button> <!-- Api undefined -->
<button value="{{ api.url_for( 'DetectRecording') }}">3</button> <!-- api undefined -->
<button value="{{ url_for( 'ns_detect.DetectRecording') }}">4</button> <!-- can't build -->
<button value="{{ url_for( 'api.DetectRecording') }}">5</button> <!-- can't build -->
<button value="{{ url_for( 'ns_detect.DetectRecording.post') }}">6</button> <!-- can't build -->
  

順便說一句:我已經安裝了
Werkzeug 2.0.3
Jinja2 3.1.2

經過反復試驗,我想通了,使url_forflask-restx一起工作需要哪個魔術鍵:
它是命名空間和修改后的類名的組合。
例子:

ns = api.namespace('xxx', path='/', description='some description')  
@ns.route('/some/endpoint')
class DoSomething(Resource):
    def post(self):
        return {"status": "ok"}

然后使用url_for({{ 'xxx_do_something' }})

也許 flask-restx 文檔會從這樣的例子中受益。

一起使用flask.Blueprintflask_restx.Namespace時,您必須在調用 flask.url_for 時引用藍圖、命名空間和路由的flask.url_for名稱。

xy所述,格式為“blueprint.namespace_route_name”。 請記住,類名稱中的駝峰式被解構為 snake_case。 (RouteName -> route_name,在上面的例子中)

例子

下面的示例將 API 注冊為flask.Blueprint ,將flask_restx.Api object 注冊為兩個命名空間(Users 和 Books),每個命名空間都有一個 GET 路由。 兩條路由都使用flask.url_for返回它們對應的端點

    
    # Create a Flask app
    app = Flask(__name__)
    
    # Create 'api' BLUEPRINT for the API, named "api"
    blueprint = Blueprint('api', __name__)
    
    # Create an instance of the Api class
    api = Api(blueprint)
    
    # Define the 'users' NAMESPACE
    users_ns = Namespace('users', description='Users related operations')
    
    # Define the 'UsersList' ROUTE in the 'users' NAMESPACE
    @users_ns.route('/')
    class UsersList(Resource):
        def get(self):
            return {'UsersList_url': url_for('api.users_users_list')}
    
    # Define the 'books' NAMESPACE
    books_ns = Namespace('books', description='Books related operations')
    
    # Define the "Books" ROUTE in the 'books' NAMESPACE
    @books_ns.route('/')
    class Books(Resource):
        def get(self):
            return {'Books_url': url_for('api.books_books')}
    
    # Register the namespaces with the API
    api.add_namespace(users_ns)
    api.add_namespace(books_ns)
    
    # Register the blueprint with the Flask app
    app.register_blueprint(blueprint)
    
    if __name__ == '__main__':
        app.run(debug=True)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM