Flask学习笔记五(与前端结合使用)

默认情况下,Flask路由响应GET请求。但是,可以通过为route()装饰器提供方法参数来更改此首选项。为了演示在URL路由中使用POST方法,首先让我们创建一个HTML表单,并使用POST方法将表单数据发送到URL。
以注册为例,我们设置一个html的注册页面,将以下脚本另存为regist.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
  <h1>用户注册</h1>
  <form method="post" action="/regist">
      <div>
          用户名:<input type="text" placeholder="账户" name="user">
      </div>

      <div>
          密码:<input type="password" placeholder="密码" name="pwd">
      </div>

      <div>
          性别:
          <input type="radio" name="gender" value="1"><input type="radio" name="gender" value="1"></div>

      <div>
          爱好:
          <input type="checkbox" name="hobby" value="10">篮球
          <input type="checkbox" name="hobby" value="20">拉丁舞
          <input type="checkbox" name="hobby" value="30">围棋
          <input type="checkbox" name="hobby" value="40">钢琴
      </div>

      <div>
          城市:
          <select name="city">
              <option value="bj">北京</option>
              <option value="sh">上海</option>
              <option value="sz">深圳</option>

          </select>
      </div>

      <input type="submit" value="提交">
  </form>
</body>
</html>

python代码如下:

# coding: utf-8

from flask import Flask,render_template,request

# 创建对象
app = Flask(__name__)
#编写路由,构造路由与函数的对应关系
@app.route("/regist",methods=["GET","POST"])
def regist():
    if request.method == "GET":
        return render_template("regist.html")
    else:
        # 接收用户通过post形式提交的数据
        print(request.form)
        user = request.form.get("user")
        pwd = request.form.get("pwd")
        gender = request.form.get("gender")
        hobby_list = request.form.getlist("hobby")
        city = request.form.get("city")
        print(user, pwd, gender, hobby_list, city)
        # 返回注册结果
        return "注册成功"

pycharm运行该程序后,在浏览器中打开regist页面,如下:
在这里插入图片描述
在html页面输入内容提交,可在服务端看到form提交的数据如下:
在这里插入图片描述
页面返回如下:
在这里插入图片描述
同理也可实现登录等其它功能,完整代码如下:

# coding: utf-8

from flask import Flask,render_template,request

# 创建对象
app = Flask(__name__)
#编写路由,构造路由与函数的对应关系
@app.route("/regist",methods=["GET","POST"])
def regist():
    if request.method == "GET":
        return render_template("regist.html")
    else:
        # 接收用户通过post形式提交的数据
        print(request.form)
        user = request.form.get("user")
        pwd = request.form.get("pwd")
        gender = request.form.get("gender")
        hobby_list = request.form.getlist("hobby")
        city = request.form.get("city")
        print(user, pwd, gender, hobby_list, city)
        # 返回注册结果
        return "注册成功"

@app.route("/login",methods=["GET","POST"])
def login():
    if request.method == "GET":
        return render_template("login.html")
    else:
        print(request.form)
        username = request.form.get("username")
        password = request.form.get("password")
        print(username,password)
        return "登录成功"

@app.route("/info")
def info():
    # return "中国联通",结合html标签设置返回样式
    #  return "<span style='color:red;'>中国</span><h1>联</h1>通"
    # Flask会自动打开这个html文件,并将内容给用户返回,默认在当前目录的templates文件夹中
    return render_template("index.html")

@app.route("/detail")
def detail():
    return render_template("detail.html")

if __name__ == '__main__':
    app.run()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值