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==""):
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。

求大神指点后台直接跳转的方法。

Python Flask框架之页面跳转的更多相关文章

  1. #3使用html+css+js制作网页 番外篇 使用python flask 框架 (I)

    #3使用html+css+js制作网页 番外篇 使用python flask 框架(I 第一部) 0. 本系列教程 1. 准备 a.python b. flask c. flask 环境安装 d. f ...

  2. Python Flask框架路由简单实现

    Python Flask框架路由的简单实现 也许你听说过Flask框架.也许你也使用过,也使用的非常好.但是当你在浏览器上输入一串路由地址,跳转至你所写的页面,在Flask中是怎样实现的,你是否感到好 ...

  3. #3使用html+css+js制作网页 番外篇 使用python flask 框架 (II)

    #3使用html+css+js制作网页 番外篇 使用python flask 框架 II第二部 0. 本系列教程 1. 登录功能准备 a.python中操控mysql b. 安装数据库 c.安装mys ...

  4. Linux ubantu中安装虚拟/使用环境virtualenv以及python flask框架

    今天学习了python flask框架的安装过程以及使用案例,感觉网上讲的东西都没有从我们这种初学者的角度去考虑(哈哈),最后还是奉上心得: 1.安装virtualenv $ sudo apt-get ...

  5. python flask框架学习——开启debug模式

    学习自:知了课堂Python Flask框架——全栈开发 1.flask的几种debug模式的方法 # 1.app.run 传参debug=true app.run(debug=True) #2 设置 ...

  6. python flask框架学习(二)——第一个flask程序

    第一个flask程序 学习自:知了课堂Python Flask框架——全栈开发 1.用pycharm新建一个flask项目 2.运行程序 from flask import Flask # 创建一个F ...

  7. python flask框架学习(一)——准备工作和环境配置与安装

    Flask装备: 学习自:知了课堂Python Flask框架——全栈开发 1.Python版本:3.6 2.Pycharm软件: 3.安装虚拟环境: (1)安装virtualenv: pip ins ...

  8. python Flask框架mysql数据库配置

    我是一个没有笔记习惯的低级程序员,但是我还是喜欢编程,从小学就开始跟着玩电脑,对抓鸡,ddos,跳板刷钻开始了自己的IT 旅程,之后学习了各种语言,但是可惜都不没有达到精通,都是略懂一二,现在想把Py ...

  9. Python Flask框架

    Python有很多Web框架,可谓是百家争鸣,我这里列出几个比较叼的几个框架 Django      市场占有率最高,官方文档几近完美,但是适合比较大的项目,小项目会显得累赘. Tornado    ...

随机推荐

  1. Cookie的有效访问路径

    Cookie 的 作用范围: Cookie详解:https://www.cnblogs.com/handsomecui/p/6117149.html 可以作用当前目录和当前目录的子目录. 但不能作用于 ...

  2. 循环删除DataTable.Row中的多行问题

    在C#中,如果要删除DataTable中的某一行,大约有以下几种办法: 1,使用DataTable.Rows.Remove(DataRow),或者DataTable.Rows.RemoveAt(ind ...

  3. (转)对存储过程进行加密和解密(SQL 2008/SQL 2012)

    原文地址:http://www.cnblogs.com/wghao/archive/2012/12/30/2837642.html 开始: 在网络上,看到有SQL Server 2000和SQL Se ...

  4. java.lang.NoSuchMethodError: org.objectweb.asm.ClassVisitor.visit(IILjava/lang/String;Ljava/lang/String;[Ljava/lang/String;Ljava/lang/String;)V

    异常完整信息 严重: Servlet.service() for servlet RegServlet threw exception java.lang.NoSuchMethodError: org ...

  5. WINDOWS权限大牛们,请进

    大家好, 我遇到一个问题,我的一台windows7去访问另一个电脑的共享,输入账号密码后,老是说密码不正确.而其他电脑去访问共享,密码账号密码后都OK 我想知道原因是什么?

  6. 人力资源管理 winform C#

    主旨思想:数据库(增,删,改,查) 资源管理器目的:实现基本人员信息   存储,调用,查看头像,修改内容. 注意事项:   1.建立两个表格  (人员表(cold,name,bumen,phone,t ...

  7. xmlreader与xmlwriter里的几个坑与解决方案

    加载超过100M的xml文件时(可能不是很常见),XmlDocument这种全部加载到内存里的模式就有点不友好了,耗时长.内存高. 这时用xmlreader就会有自行车换超跑的感觉,但其间遇到几个坑, ...

  8. Jenkins 自动化部署asp.net

    使用步骤 1.安装jenkins.git和vs,并确保机器上安装了.net framework 4.5和.net framework4.0 ,完成后访问http://localhost:8080. 2 ...

  9. Java读写配置文件prop.properties

    Java读写配置文件prop.properties @Test public void fun() throws IOException{ Properties prop=new Properties ...

  10. Weekly Contest 118

    970. Powerful Integers Given two non-negative integers x and y, an integer is powerful if it is equa ...