Form - used to accept data entered by the client. The request encapsulates the content of the HTTP request, and the form data can be obtained through request.form . Forms can be implemented in Flask by using the Flask-WTF extension.
1. Create a form class and instantiate the object
2. Use form objects in templates
3. Process the form in the view function
# Regular form get
#demo1file
#coding=utf-8
from flask import Flask,render_template ,request #render_template为模板
from config import Config
app = Flask(__name__)
# use configuration file
app.config.from_object(Config)
@app.route('/' ,method=['GET','POST'] )
def demo1():
#get form data
user = request.form.get('user')
pswd = request.form.get('pswd')
return render_template('login.html')
if __name__=='__main__':
app.run()
"""login.html under templates in the same level directory of demo1"""
<!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 sibling file config.py"""
class Config:
DEBUG = True