一、
建立py文件 (可以为1flask,不要只写关键字例如:flask)
在Terminal 执行语句 pip install flask 实现效果如下
二、
建立好后在文本编辑栏输入以下代码
# coding=utf-8
from flask import Flask , request
# 创建Flask类对象
app = Flask(__name__)
'''
通过装饰器将url和函数进行绑定,在访问根url时,将该请求交给index()函数处理
'''
'''
@xxx 装饰器 (白酒)
路由 (茅台)
'''
@app.route('/')
def index():
# 返回字符串,表示在页面上显示该字符串
return 'Hello, World!'
@app.route('/login2' , methods=['post'])
def login():
# 获取 URL 参数
# username = request.args.get("username")
# password = request.args.get("password")
# 获取 post 请求参数
username = request.form.get("username")
password = request.form.get("password")
print("用户名: ",username,",密码: ",password)
return "登录成功"
if __name__ == "__main__":
# 启动
app.run()
运行结果如图
点击蓝色部分网址查看浏览器是否运行
三、
在此文件(与上述py文件归属相同文件夹)中建立一个新文件夹(取名可为static)
在static文件中建立html文件[(取名可为login2)取名参数要与py文件中定义的参数@app.route('/login2' ,引号中的值相同]
之后在文件中写入需要的代码
例如
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style type="text/css">
*{
margin: 0;
padding: 0;
}
body{
font-size:12px ;
}
.box{
margin-top: 40px;
}
.box .box1{
width: 1210px;
height: 76px;
margin: 0 auto;
}
.box .logo{
display: inline-block;
width: 170px;
height: 60px;
background-image:url(img/logo-201305-b.png) ;
float: left;
}
.box .box2 .title{
margin-left: 20px;
color: #333333;
top: 12px;
position: relative;
font-size:24px ;
line-height: 50px;
}
.box .login{
float: right;
color: #999;
font-size: 16px;
position: relative;
top: 20px;
}
.box .login a{
text-decoration: none;
color: #E1251B;
}
.denglu{
width: 400px;
height: 304px;
margin: 0 auto;
margin-top: 80px;
}
.denglu .shu1 {
width: 398px;
height: 52px;
margin-bottom: 30px;
border: 1px solid #999999;
}
.denglu .shu1 input{
margin:18px 0 18px 12px;
height: 16px;
width: 200px;
outline: none;
border: 0 solid;
}
.denglu .de{
display: block;
width: 398px;
height: 52px;
color: #FFFFFF;
background-color: #E1251B;
font-size: 16px;
text-align: center;
line-height: 52px;
cursor: pointer;
text-decoration: none;
border:0;
}
</style>
</head>
<body>
<div class="box">
<div class="box1">
<div class="box2">
<a href="#" class="logo"></a>
<span class="title">欢迎登录</span>
<span class="login">已有账号?<a href="#">请登录></a></span>
</div>
</div>
</div>
<div class="denglu">
<form action="/login2" method="post">
<div class="shu1">
<label for="aa" ></label>
<input type="text" id="aa" name="username" placeholder="请输入用户名"/>
</div>
<div class="shu1">
<label for="bb" ></label>
<input type="password" id="bb" name="password" placeholder="请输入密码"/>
</div>
<div class="shu1">
<label for="cc" ></label>
<input type="password" id="cc" name="password" placeholder="请输入确认密码"/>
</div>
<input class="de" type="submit" value="登录">
</form>
</div>
</body>
</html>
录入后再次打开 1flask 文件 重新运行(每次对代码进行更好都要重新运行)
运行结果依旧为
打开蓝色部分网址
在此区域5000后面写“/static/login2.html”并敲回车
即可得到以下结果
在输入框中写入账号、密码信息点击提交(或敲回车键)可得到结果如下
随后返回Python文件 可以看到返回值