1
我有一些以landingpage
開頭並以唯一ID結尾的URL。我需要能夠從URL獲取id,以便我可以將另一個系統的一些數據傳遞給我的Flask應用程序。我怎樣才能得到這個價值?從Flask路徑中的URL獲取變量
http://localhost/landingpageA
http://localhost/landingpageB
http://localhost/landingpageC
我有一些以landingpage
開頭並以唯一ID結尾的URL。我需要能夠從URL獲取id,以便我可以將另一個系統的一些數據傳遞給我的Flask應用程序。我怎樣才能得到這個價值?從Flask路徑中的URL獲取變量
http://localhost/landingpageA
http://localhost/landingpageB
http://localhost/landingpageC
這回答了文檔的quickstart。
您需要一個變量url,您可以通過在url中指定<name>
並在視圖函數中接受name
來創建該變量。
@app.route('/landingpage<id>')
def landing_page(id):
...
# /landingpageA
更典型的URL部分用/
分開。
@app.route('/landingpage/<id>')
def landing_page(id):
...
# /landingpage/A
您還可以通過該值作爲get it from the request查詢字符串的一部分,並且,雖然如果它總是需要的,最好使用變量像上面。
from flask import request
@app.route('/landingpage')
def landing_page():
id = request.args['id']
...
# /landingpage?id=A
[The docs](http://flask.pocoo.org/docs/0.10/quickstart/#variable-rules)會有幫助! – Monkpit