IDE用的PyCharm(還是vs強大啊)。
項目結構:


2:頁面:
<!doctype html> <html lang="zh"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>用戶登錄</title><link rel="shortcut icon" href="{{ url_for('static', filename='images/lock.png')}}"/> </head> <body> <div align="center"> <div> <div> <h1>Welcome</h1> <form class="form" method="post" action="" onsubmit="return Login();"> <input type="text" placeholder="Username" name="username" id="username"> <input type="password" placeholder="Password" name="password" id="password"> <button type="submit" id="login-button">SubmitLogin</button> <button type="button" id="btnlogin">ButtonLogin</button> <a href="infor">testhtm</a> </form> </div> </div> </div> <script type="text/javascript" src="{{ url_for('static', filename='jquery.min.js') }} "></script> {#<script src="http://cdn.static.runoob.com/libs/jquery/1.10.2/jquery.min.js"></script>#} <script type="text/javascript"> function Login() { var login = false; var txtname = $("#username").val(); var txtpsw = $("#password").val(); $.ajax({ {#url: "/py_login",可以#} url:"{{url_for("py_login") }}",{# 可以調到后台,路由名稱#} {#data: "username=" + txtname + "&password=" + txtpsw,#} data:{"username":txtname,"userpswd":txtpsw}, type: 'GET', contentType: 'json',{#'application/x-www-form-urlencoded',#} async: false, success: function (d) { alert("success!"+d) var status = d.status; if (d != "undefined" && d=="success") { window.location.href="infor";{# 這里鏈接的應該是路由中的名稱#} } else { alert("登錄失敗!") } } }); return login; } </script> </body> </html>
3:后台FlaskWeb.py文件。
from flask import Flask, render_template, redirect, request, url_for , send_file import pymssql app = Flask(__name__) # app.debug = True; # @app.route('/') @app.route('/') def index(): return render_template('login.html') # send_file('index.html') @app.route('/py_login',methods=['GET','POST']) def py_login(): if request.method == 'GET': print(request.form) txtname = request.args.get('username') txtpswd = request.args.get('userpswd') if(txtname=="longdb" and txtpswd=="123"): return "success" # get可以到這里 print(url_for('/testhtm')) # return redirect(url_for('/testhtm')) else: return "enterfailed" @app.route('/infor') #頁面鏈接該路由名稱 def f_infor(): return render_template('infor.html') # send_file('/templates/testhtm.html') # # 函數 @app.route('/insertdata',methods=['GET','POST']) def py_insertdata(): # noinspection PyBroadException try: # conn = pymssql.connect(server='longdabing',user='sa',password='sasa',database='longtest') conn = pymssql.connect('longdabing', 'sa', 'sasa', 'longtest') cursor = conn.cursor() txtno = request.args.get('hno') txtname = request.args.get('hname') txtobject = request.args.get('hobject') txtscore = request.args.get('hscore') sql = "INSERT dbo.infor( no, name, object, score )VALUES ( '"+txtno+"','"+txtname+"','"+txtobject+"','"+txtscore+"')" cursor.execute(sql) conn.commit() # 提交數據到數據庫,否則不會插入到數據庫中 conn.close() return "insertok" except Exception as e: conn.rollback() # 回滾操作 return "insertfailed" raise e # finally: # conn.close() def getdata(sql): # conn = pymssql.connect(server='longdabing',user='sa',password='sasa',database='longtest') conn = pymssql.connect('longdabing', 'sa', 'sasa', 'longtest') cursor = conn.cursor() cursor.execute(sql) row = cursor.fetchone() while row: print("ID=%d, No=%s, Name=%s, Object=%s, Score=%s" % (row[0], row[1], row[2], row[3], row[4])) row = cursor.fetchone() conn.close() return if __name__ == '__main__': app.run()
注明:運行FlaskWeb.py文件時,路由longin.html頁面,進入該頁面后,輸錄入賬號和密碼,驗證通過的話,則通過window.location.href="infor";{# 這里鏈接是路由中的名稱#} 來跳轉頁面,最終還是由后台加載頁面 infor.html。
本來想在用戶和密碼驗證通過后,用 redirect()方法跳轉到 infor.html頁面的,但是都失敗,所以才用了window.location.href。
求大神指點后台直接跳轉的方法。