flask—常规表单获取

表单—用来接受客户端输入的数据。request封装了HTTP请求内容,可以通过request.form来获取表单数据。在Flask中可以通过使用Flask- WTF扩展来实现表单。

1、创建表单类,实例化对象

2、在模板中使用表单对象

3、在视图函数中处理表单

# 常规表单获取

#demo1文件

#coding=utf-8

from flask import Flask,render_template ,request    #render_template为模板

from config import Config

app = Flask(__name__)

#使用配置文件

app.config.from_object(Config)

@app.route('/' ,method=['GET','POST'] )

def demo1():

      #获取表单数据

     user = request.form.get('user')

     pswd = request.form.get('pswd')

      return render_template('login.html')

if __name__=='__main__':

     app.run()

"""demo1同级目录templates下的login.html"""

<!DOCTYPE html>

<html lang="en">

<head>

         <meta charset="UTF-8">

          <title>title</title>

</head>

<body>

       <form method="post">

              <p><input type = "text" name="user" placehoder="User" size="35"></p>

              <p><input type = "password" name="pswd" placehoder="Password" size="35"></p>

              <p><input type="submit"></p>

</form>

</body>

</html>

"""demo1同级文件config.py"""

class  Config:

      DEBUG = True

猜你喜欢

转载自my.oschina.net/u/3753872/blog/1649940