• python+flask框架——前后端数据传递


    从前端传向后端:

    前端:

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta http-equiv="X-UA-Compatible" content="ie=edge">
        <title>Document</title>
    </head>
    <body>
        <form action="/login" method="post">
            <p>账号:<input type="text" name="username" ></p>
            <p>密码:<input type="password" name="password" ></p>
            <button type="submit">登录</button>
        </form>
        <br>
        <form action="/resign1" method="post">
             <button type="submit">注册</button>
        </form>
    
    </body>
    </html>
    

      

    app.py:

    @app.route('/login',methods=['post'])
    def login():
        user_info = request.values.to_dict()
        username = user_info.get("username")
        password = user_info.get("password")
        print(password,username)
        sql1 = "select * from user where username="+username+" and password = "+password+""
        res = db.selectone(sql=sql1)
        if res!=None:
            print(res[0], res[1]) 
            return render_template('index.html')
        else:
            return render_template('resign.html')
    

      

    从后端传向前端:

    在return的时候,返回.html的同时,返回一个变量:

    return render_template('index.html',img=read_img) #read_img是app.py中的一个方法,我这里是读取本地图片
    

      

    def read_img():
        image_path = './image'
        imglist = get_img_list(image_path, [], 'png')
        imgall = []
        for imgpath in imglist:
            # print(imgpath)
            imaname = os.path.split(imgpath)[1]  # 分离文件路径和文件名后获取文件名(包括了后缀名)
            # print(imaname)
            img = cv2.imread(imgpath, cv2.IMREAD_COLOR)
            imgall.append(img)
            #cv2.namedWindow(imaname, cv2.WINDOW_AUTOSIZE)
            #cv2.imshow(imaname, img)
        #cv2.waitKey(0)
    
        return imgall[]
    

      前端使用{{img}}进行接收,获取的就是read_img()函数中返回的内容 例如:

    <p>{{img}}</p>
    

      

  • 相关阅读:
    PHP防采集方法代码
    Borland C++ Builder 编译绿色Exe程序
    关于结构体构造函数使用总结
    Ubuntu18下移植飞凌的QT4.8.5
    qt在arm平台中,把鼠标指针消失。
    ubuntu下gcc g++操作
    Ubuntu下Qt_Creator支持搜狗中文输入
    ubuntu18.04.1降级交叉编译器 arm-linux-gcc-4.4.3
    error: narrowing conversion of '4323168000' from 'long int' to 'float' inside { } [-Wnarrowing] }; ^
    #pragma pack(1)的意义
  • 原文地址:https://www.cnblogs.com/zhangxinyue/p/14839626.html
Copyright © 2020-2023  润新知