在 Flask 框架中,路由是通过 Flask 类提供的 @app.route 装饰器来实现的。该装饰器会将一个函数注册为特定的 URL。
例如,下面是一个简单的 Flask 应用程序,其中定义了一个路由,使得当用户请求网站的根 URL 时,返回一个欢迎信息:
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello():
return 'Hello, World!'
在这个例子中,使用了 @app.route('/') 装饰器将 hello 函数注册为应用程序的根 URL。当用户访问应用程序的根 URL 时,Flask 框架将会调用 hello 函数,并返回该函数的返回值。
在 Flask 框架中,还可以使用变量来创建动态路由,例如:
@app.route('/user/')
def show_user_profile(username):
return 'User %s' % username
在这个例子中,使用了 @app.route('/user/
通过这种方式,Flask 框架可以方便地处理各种 URL 请求,并将其路由到相应的处理函数。
页面更新:2024-04-29
本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828
© CopyRight 2008-2024 All Rights Reserved. Powered By bs178.com 闽ICP备11008920号-3
闽公网安备35020302034844号