查看“Python Flask:路由”的源代码
←
Python Flask:路由
跳转至:
导航
,
搜索
因为以下原因,您没有权限编辑本页:
您所请求的操作仅限于该用户组的用户使用:
用户
您可以查看与复制此页面的源代码。
==定义== 处理URL和函数之间关系的程序 称为路由, 而这个函数称为视图函数。 ==Flask中使用路由== 使用程序实例提供的app.route装饰器, 把装饰的函数注册为路由: <nowiki> from flask import Flask app = Flask(__name__) @app.route('/hello') def hello_world(): return '你好' if __name__ == '__main__': #app.debug = True app.run() </nowiki> 当浏览器访问 http://127.0.0.1:5000/hello 时,URL会触发hello_world()函数,执行函数体中的代码。 ===给URL添加变量=== <nowiki> from flask import Flask app = Flask(__name__) @app.route('/user/<username>') def show_user_profile(username): return 'User: %s' % username @app.route('/post/<int:post_id>') def show_post(post_id): return 'Post ID: %d' % post_id if __name__ == '__main__': #app.debug = True app.run()</nowiki> 访问http://127.0.0.1:5000/user/张三 时就会触发show_use_profile函数 访问http://127.0.0.1:5000/post/123 时就会触发show_post函数 show_post()函数使用了转换器,它有下面几种类型: *Int *float *path 代码中使用int:post_id将其设置为整形
返回至
Python Flask:路由
。
导航菜单
个人工具
登录
命名空间
页面
讨论
变种
视图
阅读
查看源代码
查看历史
更多
搜索
导航
首页
最近更改
随机页面
帮助
工具
链入页面
相关更改
特殊页面
页面信息