Flask——路由



现代 Web 框架使用路由技术来帮助用户记住应用程序 URL。直接从主页访问所需页面,而无需进行导航,这十分有用。

Flask 中的 route() 装饰器用于将 URL 绑定到函数。例如:-

@app.route(‘/hello’)
def hello_world():
   return ‘hello world’

在此,URL ‘/hello’ 规则绑定到了 hello_world() 函数。因此,如果用户访问 https://:5000/hello URL,则 hello_world() 函数的输出将在浏览器中呈现。

应用程序对象的 add_url_rule() 函数也可以用来将 URL 与函数绑定,正如上述示例中使用的 route()

以下表示也用于装饰器的目的:

def hello_world():
   return ‘hello world’
app.add_url_rule(‘/’, ‘hello’, hello_world)
广告
© . All rights reserved.