2016-03-15 71 views
0

我知道當燒瓶構建大型應用程序時,它已經註冊了多個藍圖。我可以在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')

所以它是從瓶教程建設大型應用程序的工作流程。這樣對嗎?


讓我們回到重點。是否有可能建立三層端點爲url_for('api. users.login')

如何打包blueprint或flask應用程序以完成我想要的結構?是可用的麼?

回答

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() 
+0

我知道這一招,但不是好主意。 – Tony

相關問題