最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
Python的flask常用函数route()介绍
时间:2022-09-26 编辑:坚强 来源:一聚教程网
本文为大家带来了关于Python的flask常用函数route()介绍,感兴趣的小伙伴一起来看看吧。
一、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/') 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()介绍的全部内容了,感兴趣的小伙伴记得点击关注哦。
相关文章
- Golang ProtoBuf的基本语法详解 10-20
- Python识别MySQL中的冗余索引解析 10-20
- Python+Pygame绘制小球代码展示 10-18
- Python中的数据精度问题介绍 10-18
- Python随机值生成的常用方法介绍 10-18
- python3解压缩.gz文件分析 09-27