我可以在Flask中构建两层以上的端点吗?

时间:2016-03-15 09:57:53

标签: python flask

我知道当flask建立大型应用程序时,它已经注册了多个蓝图。

烧瓶蓝图开始初始化蓝图对象,它同时声明了第一层端点的名称。

例如:

users_bp = Blueprint('users', __name__)

根据表达式,users_bp的第一个终结点名称为users

蓝图对象继续注册其视图函数,它同时声明了第二层端点的名称。

@users_bp.route('/login')
    def login():
        # do something

根据表达式,users_bp的第二层端点名称为login,它来自视图名称。

如果我想使用endpoint来获取相应的网址,我应该这样做:url_for('users.login')

所以它是从flask教程构建大型应用程序的工作流程。是不是?

让我们回过头来看看。是否可以构建三层端点为url_for('api. users.login')

如何打包蓝图或烧瓶应用程序以完成我想要的结构?可以吗?

1 个答案:

答案 0 :(得分:0)

您可以在路径装饰器中设置端点,例如:

from flask import Flask, render_template_string

app = Flask(__name__)


@app.route('/', endpoint="this.is.the.home.endpoint")
def index():
    _html="<a href='{{url_for('this.is.another.endpoint')}}'>Go to another endpoint</a>"
    return render_template_string(_html)


@app.route('/another', endpoint="this.is.another.endpoint")
def another():
    _html="<a href='{{url_for('this.is.the.home.endpoint')}}'>Go to the home page</a>"
    return render_template_string(_html)

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