Flask的URL規則基于werkzeug的路由模塊,
用來保證URL的唯一性。
例如帶斜線:
@app.route('/example/')
def example():return 'ok'
如果訪問一個結尾不帶斜線的URL會被重定向到斜線的URL上。
(/example)變為(/example/)
如果不帶斜線:
@app.route('/index')
def index():return 'ok'
上例子最后不帶斜線,如果我們訪問一個帶斜線的(/index/)
就會產生一個404“Not Found”的錯誤。
@app.route('/', endpoint='1')
不能重名
endpoint 的值是唯一的,同一模塊中可以有同名的 view function (視圖函數)。對于 url_for 函數的參數,如果使用函數名作為參數,則無法確定其 url ;使用 endpoint 作為參數,則保證了 url_for 返回確定的 url 。flask.url_for 需要通過 endpoint 得到 url ,可以避免匿名函數的問題。
<code># encoding: utf-8
from flask import Flaskapp=Flask(__name__)@app.route('/',endpoint="good")
def index():
return "Good jod"@app.route('/<int:id>',endpoint="bad")
def index(id):
return "%s"%idif __name__ == "__main__":
app.run()
</code>
?