0
点赞
收藏
分享

微信扫一扫

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 Flask

app=Flask(__name__)

@app.route('/',endpoint="good")
def index():
return "Good jod"

@app.route('/<int:id>',endpoint="bad")
def index(id):
return "%s"%id

if __name__ == "__main__":
app.run()
</code>

 


举报

相关推荐

0 条评论