“Python Flask:路由”的版本间的差异
来自CloudWiki
(创建页面,内容为“==定义== 处理URL和函数之间关系的程序 称为路由, 而这个函数称为视图函数。 ==Flask中使用路由== 使用程序实例提供的app.rou…”) |
|||
第53行: | 第53行: | ||
代码中使用int:post_id将其设置为整形 | 代码中使用int:post_id将其设置为整形 | ||
+ | |||
+ | ===为视图绑定多个URL=== | ||
+ | 一个视图可以绑定多个URL, | ||
+ | |||
+ | <nowiki> | ||
+ | from flask import Flask | ||
+ | app = Flask(__name__) | ||
+ | |||
+ | @app.route('/') | ||
+ | @app.route('/index') | ||
+ | def index(): | ||
+ | return "Welcome to Flask" | ||
+ | |||
+ | if __name__ == '__main__': | ||
+ | app.run() | ||
+ | </nowiki> | ||
+ | |||
+ | 访问 http://127.0.0.1:5000/index 和http://127.0.0.1:5000/ 都可以访问网页 | ||
+ | |||
+ | ===构建URL=== | ||
+ | Flask除了能够匹配URL ,还能生成URL, | ||
+ | |||
+ | Flask可以用url_for()来给指定的函数构造URL. | ||
+ | |||
+ | <nowiki> | ||
+ | from flask import Flask,url_for | ||
+ | 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): | ||
+ | # 根据ID显示文章,ID是整型数据 | ||
+ | return 'Post ID: %d' % post_id | ||
+ | |||
+ | @app.route('/url/') | ||
+ | def redirect_to_url(): | ||
+ | # 跳转到show_post()视图函数 | ||
+ | print(url_for('show_post', post_id=10)) | ||
+ | return url_for('show_post', post_id=10) | ||
+ | |||
+ | if __name__ == '__main__': | ||
+ | app.run()</nowiki> | ||
+ | |||
+ | ===HTTP方法=== | ||
+ | 默认情况下,路由只回应GET请求 | ||
+ | |||
+ | 但是通过route()装饰器 传递methods参数可以改变这个行为。 | ||
+ | |||
+ | @app.route('/login',methods=['GET','POST']) |
2022年1月13日 (四) 04:14的版本
定义
处理URL和函数之间关系的程序 称为路由,
而这个函数称为视图函数。
Flask中使用路由
使用程序实例提供的app.route装饰器,
把装饰的函数注册为路由:
from flask import Flask app = Flask(__name__) @app.route('/hello') def hello_world(): return '你好' if __name__ == '__main__': #app.debug = True app.run()
当浏览器访问 http://127.0.0.1:5000/hello 时,URL会触发hello_world()函数,执行函数体中的代码。
给URL添加变量
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()
访问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将其设置为整形
为视图绑定多个URL
一个视图可以绑定多个URL,
from flask import Flask app = Flask(__name__) @app.route('/') @app.route('/index') def index(): return "Welcome to Flask" if __name__ == '__main__': app.run()
访问 http://127.0.0.1:5000/index 和http://127.0.0.1:5000/ 都可以访问网页
构建URL
Flask除了能够匹配URL ,还能生成URL,
Flask可以用url_for()来给指定的函数构造URL.
from flask import Flask,url_for 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): # 根据ID显示文章,ID是整型数据 return 'Post ID: %d' % post_id @app.route('/url/') def redirect_to_url(): # 跳转到show_post()视图函数 print(url_for('show_post', post_id=10)) return url_for('show_post', post_id=10) if __name__ == '__main__': app.run()
HTTP方法
默认情况下,路由只回应GET请求
但是通过route()装饰器 传递methods参数可以改变这个行为。
@app.route('/login',methods=['GET','POST'])