您现在的位置是:网站首页> 编程资料编程资料
Python的flask常用函数route()_python_
2023-05-26
183人已围观
简介 Python的flask常用函数route()_python_
一、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()的文章就介绍到这了,更多相关Python flask 内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
您可能感兴趣的文章:
相关内容
- 利用Python自动生成PPT的示例详解_python_
- python+selenium 实现扫码免密登录示例代码_python_
- Python+OpenCV实现图像识别替换功能详解_python_
- Python中Timedelta转换为Int或Float方式_python_
- python的endswith()的使用方法及实例_python_
- python 对excel交互工具的使用详情_python_
- Python读取hdf文件并转化为tiff格式输出_python_
- Python利用Turtle绘画简单图形_python_
- Python OpenCV中的drawMatches()关键匹配绘制方法_python_
- Python cv.Canny()方法参数与使用方法_python_
