• 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>
    

      

  • 相关阅读:
    Pycharm5注册方式
    五、监听共享目录文件
    三、python webservice
    二、Python安装扩展库
    一、Python安装下载
    test
    拖延
    要乐观对待生活
    乞讨者
    不要总是指责和埋怨
  • 原文地址:https://www.cnblogs.com/zhangxinyue/p/14839626.html
Copyright © 2020-2023  润新知