Flask唯一URL规则--@app.route('/', endpoint='1')
生活随笔
收集整理的這篇文章主要介紹了
Flask唯一URL规则--@app.route('/', endpoint='1')
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
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>?
總結
以上是生活随笔為你收集整理的Flask唯一URL规则--@app.route('/', endpoint='1')的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: wps文件一点打印就关闭打印机服务器,W
- 下一篇: SPI协议