Python Flask框架之页面跳转
IDE用的PyCharm(还是vs强大啊)。
项目结构:


<!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框架之页面跳转的更多相关文章
- #3使用html+css+js制作网页 番外篇 使用python flask 框架 (I)
#3使用html+css+js制作网页 番外篇 使用python flask 框架(I 第一部) 0. 本系列教程 1. 准备 a.python b. flask c. flask 环境安装 d. f ...
- Python Flask框架路由简单实现
Python Flask框架路由的简单实现 也许你听说过Flask框架.也许你也使用过,也使用的非常好.但是当你在浏览器上输入一串路由地址,跳转至你所写的页面,在Flask中是怎样实现的,你是否感到好 ...
- #3使用html+css+js制作网页 番外篇 使用python flask 框架 (II)
#3使用html+css+js制作网页 番外篇 使用python flask 框架 II第二部 0. 本系列教程 1. 登录功能准备 a.python中操控mysql b. 安装数据库 c.安装mys ...
- Linux ubantu中安装虚拟/使用环境virtualenv以及python flask框架
今天学习了python flask框架的安装过程以及使用案例,感觉网上讲的东西都没有从我们这种初学者的角度去考虑(哈哈),最后还是奉上心得: 1.安装virtualenv $ sudo apt-get ...
- python flask框架学习——开启debug模式
学习自:知了课堂Python Flask框架——全栈开发 1.flask的几种debug模式的方法 # 1.app.run 传参debug=true app.run(debug=True) #2 设置 ...
- python flask框架学习(二)——第一个flask程序
第一个flask程序 学习自:知了课堂Python Flask框架——全栈开发 1.用pycharm新建一个flask项目 2.运行程序 from flask import Flask # 创建一个F ...
- python flask框架学习(一)——准备工作和环境配置与安装
Flask装备: 学习自:知了课堂Python Flask框架——全栈开发 1.Python版本:3.6 2.Pycharm软件: 3.安装虚拟环境: (1)安装virtualenv: pip ins ...
- python Flask框架mysql数据库配置
我是一个没有笔记习惯的低级程序员,但是我还是喜欢编程,从小学就开始跟着玩电脑,对抓鸡,ddos,跳板刷钻开始了自己的IT 旅程,之后学习了各种语言,但是可惜都不没有达到精通,都是略懂一二,现在想把Py ...
- Python Flask框架
Python有很多Web框架,可谓是百家争鸣,我这里列出几个比较叼的几个框架 Django 市场占有率最高,官方文档几近完美,但是适合比较大的项目,小项目会显得累赘. Tornado ...
随机推荐
- sencha:日期选择组件datepicker
来源于<sencha touch权威指南> ------------------------------- 除app.js外,其它内容都与上一篇博客里的内容相同.app.js代码如下: E ...
- codefirst 关系处理
1.http://www.cnblogs.com/libingql/archive/2013/01/31/2888201.html 2.多对多 protected override void OnMo ...
- SpringMVC——异常处理
Spring MVC 通过 HandlerExceptionResolver 处理程序的异常,包括 Handler 映射.数据绑定以及目标方法执行时发生的异常. SpringMVC 提供的 Handl ...
- 什么是Condition Number(条件数)?
In the field of numerical analysis, the condition number of a function with respect to an argument m ...
- Js 正则获取Html元素
var html = $("#summaryTemplate").html(); var imageMath = /<img [^<,>]*(?=target-t ...
- asp.net 设置分页
private const int PAGESIZE = 5; //定义每页有五行数据 private void FillPageList() { int pageCount = 0; // page ...
- php 可变数量的参数列表
可变数量的参数列表 PHP 在用户自定义函数中支持可变数量的参数列表.在 PHP 5.6 及以上的版本中,由 ... 语法实现:在 PHP 5.5 及更早版本中,使用函数func_num_args() ...
- 认识学习MVC这家伙
通过最近的学习认识,只能感慨这玩意太强大了! 以前看了一些MVC的入门教程,看入门教程我感觉不能去体会它的强大,可以看看它的扩展点,通过扩展点去真正的试着了解它,体会它的强大. 它的验证.模型绑定提供 ...
- sqlite数据库文件查看
- Effective Java笔记
chapter 1 java支持四种类型:interface,class,array,primitive(基本类型) chapter 2 创建对象方式: ①构造器 ②静态工厂方法代替构造器:名称可以按 ...