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>

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM