2025年python框架之flak学习笔记

python框架之flak学习笔记一 简介 flask 是一个轻量级框架 内置 WSGI 服务 更多的功能依靠第三方库 需要什么安装什么东西 可以参考官方网站 WSGI Web 服务器网关接口 Python Web Server Gateway Interface 缩写为 WSGI

大家好,我是讯享网,很高兴认识大家。

一、简介
flask是一个轻量级框架;内置WSGI服务;
更多的功能依靠第三方库,需要什么安装什么东西;可以参考官方网站。

WSGI:Web服务器网关接口Python Web Server Gateway Interface,缩写为WSGI)是为Python语言定义的Web服务器和Web应用程序或框架之间的一种简单而通用的接口。自从WSGI被开发出来以后,许多其它语言中也出现了类似接口。

二、简单启动步骤

app.py文件

# 1、导入Flask扩展 from flask import Flask # 2、创建Flask应用实例,需要传入__name__,为了确定资源所在的路径 app = Flask(__name__) #3、定义路由及视图函数,通过装饰器实现 @app.route('/') def hello_world():     return '你好!' #4、启动程序 if __name__ == '__main__':     #执行了app.run()就会将flask运行在一个简易的服务器上,Flask提供测试用的。     app.run()

讯享网
讯享网#定义路由及视图函数,通过装饰器实现,路由默认只支持get @app.route('/',methods=["GET","POST"]) def index():     return '你好!' @app.route("/orders/<int:order_id>") def order(order_id):     #在视图函数的()中填入参数名,后面在函数中才能调用     a = (f"订单ID是:{order_id}")     #参数类型是字符串,有的时候需要对路由做访问优化     return a

2、jinja2模板引擎
 第一:如何返回一个网页
static:可以把图片
templates:网页模板


讯享网

render_template 导入模板命令

from flask import Flask,render_template

app = Flask(__name__)

@app.route('/',methods=["GET","POST"])
def index():
    return render_template('index.html')

if __name__ == '__main__':
    app.run()
讯享网from flask import Flask,render_template

app = Flask(__name__)

@app.route('/')
def index():
    #传进来的为字符串
    url_str="www.baidu.com"
    # 传进来的为列表
    my_list=[1,2,3,4,5]
    #传进来的为字典
    my_dict={
        "name":"heima",
        "url":'www.itheima.com'
    }

    return render_template('index.html',url_str=url_str,my_list=my_list,my_dict=my_dict)

if __name__ == '__main__':
    app.run()
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>第一个flask网页</title>
</head>
<body>
    <h1>The first flask html file.</h1>
    <!--语法,如何输出-->
    <h2>get进来的字符串数据:</h2>
    {
  
  
  
  
  
  
  
    
  { url_str }}<br> <h2>get进来的列表数据:</h2> { 
  
    
  { my_list }}<br> <h2>get进来的字典数据:</h2> { 
  
    
  { my_dict }}<br>
</body>
</html>

四、request和重定向的使用

讯享网from flask import Flask,render_template,request,redirect,url_for

app = Flask(__name__)


@app.route('/',methods=["GET","POST"])
def hello_world():
    #request的用法
    if request.method == "GET":
        return render_template("index.html")
    if request.method == 'POST':
        user = request.form.get("user")
        pw = request.form.get("password")
        aa =f"账号{user},密码{pw}"
        return aa
#重定向redirect
@app.route("/hello")
def hello():
    return redirect("https://www.baidu.com")

@app.route("/hi")
def hi():
    return redirect(url_for("hello"))

if __name__ == '__main__':
    app.run()

五、数据传输

#flask数据传输 app.config['JSON_AS_ASCII']=False #关键的地方,python处理中文的方法 @app.route('/') def index(): data={ 'name':'张三' } #第一种处理json的方法 # response= make_response(json.dumps(data,ensure_ascii=False)) # response.mimetype ="application/json" # return response #第二种处理json的方法 return jsonify(data) if __name__ == '__main__': app.run() 

小讯
上一篇 2025-04-04 08:02
下一篇 2025-01-04 23:09

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/32552.html