Python的flask常用函数route()
更新时间:2022年07月14日 17:05:10 投稿:hqx
这篇文章主要介绍了Python的flask常用函数route(),文章围绕主题展开详细的内容介绍,具有一定的参考价值,需要的小伙伴可以参考一下
(福利推荐:【腾讯云】服务器最新限时优惠活动,云服务器1核2G仅99元/年、2核4G仅768元/3年,立即抢购>>>:9i0i.cn/qcloud)
(福利推荐:你还在原价购买阿里云服务器?现在阿里云0.8折限时抢购活动来啦!4核8G企业云服务器仅2998元/3年,立即抢购>>>:9i0i.cn/aliyun)
一、route()路由概述
- 功能:将URL绑定到函数
- 路由函数route()的调用有两种方式:静态路由和动态路由
二、静态路由和动态路径
方式1:静态路由
@app.route(“/xxx”) xxx为静态路径 如::/index / /base等,可以返回一个值、字符串、页面等
from flask import Flask app = Flask(__name__) @app.route('/hello') def hello_world(): return 'Hello World!!!' @app.route('/pro') def index(): return render_template('login.html') if __name__ == '__main__': app.run(debug = True)
方式2:动态路由
采用<>进行动态url的传递
@app.route(“/”),这里xxx为不确定的路径。
from flask import Flask app = Flask(__name__) @app.route('/hello/<name>') def hello_name(name): return 'Hello %s!' % name if __name__ == '__main__': app.run(debug = True)
- 如果浏览器地址栏输入:
http:// localhost:5000/hello/w3cschool
- 则会在页面显示:
Hello w3cschool!
三、route()其它参数
1.methods=[‘GET’,‘POST’]
- 当前视图函数支持的请求方式,不设置默认为GET
- 请求方式不区分大小写
- methods=[‘GET’] 支持的请求方法为GET
- methods=[‘POST’] 支持的请求方法为POST
- methods=[‘GET’,‘POST’] 支持的请求方法为POST GET
@app.route('/login', methods=['GET', 'POST']) # 请求参数设置不区分大小写,源码中自动进行了upper def login(): if request.method == 'GET': return render_template('login.html') elif request.method == 'POST': username = request.form.get('username') pwd = request.form.get('pwd') if username == 'yang' and pwd == '123456': session['username'] = username return 'login successed 200 ok!' else: return 'login failed!!!'
到此这篇关于Python的flask常用函数route()的文章就介绍到这了,更多相关Python flask 内容请搜索程序员之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持程序员之家!
相关文章
关于torch.scatter与torch_scatter库的使用整理
这篇文章主要介绍了关于torch.scatter与torch_scatter库的使用整理,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教2023-09-09
最新评论