Flask唯一URL规则--@app.route('/', endpoint='1')
生活随笔
收集整理的這篇文章主要介紹了
Flask唯一URL规则--@app.route('/', endpoint='1')
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
Flask的URL規(guī)則基于werkzeug的路由模塊,
用來保證URL的唯一性。
例如帶斜線:
@app.route('/example/') def example():return 'ok'如果訪問一個(gè)結(jié)尾不帶斜線的URL會(huì)被重定向到斜線的URL上。
(/example)變?yōu)?/example/)
如果不帶斜線:
@app.route('/index') def index():return 'ok'上例子最后不帶斜線,如果我們?cè)L問一個(gè)帶斜線的(/index/)
就會(huì)產(chǎn)生一個(gè)404“Not Found”的錯(cuò)誤。
@app.route('/', endpoint='1')
不能重名
endpoint 的值是唯一的,同一模塊中可以有同名的 view function (視圖函數(shù))。對(duì)于 url_for 函數(shù)的參數(shù),如果使用函數(shù)名作為參數(shù),則無法確定其 url ;使用 endpoint 作為參數(shù),則保證了 url_for 返回確定的 url 。flask.url_for 需要通過 endpoint 得到 url ,可以避免匿名函數(shù)的問題。
<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>?
總結(jié)
以上是生活随笔為你收集整理的Flask唯一URL规则--@app.route('/', endpoint='1')的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: wps文件一点打印就关闭打印机服务器,W
- 下一篇: SPI协议