步骤
一、创建test文件夹
二、在test文件夹下创建main.py文件和templates文件夹
main.py代码如下:
1 # -*- condig:utf-8 -*- 2 3 from flask import Flask,render_template,request 4 5 app = Flask(__name__) 6 7 @app.route('/login') 8 def index(): 9 # return 'hello Flask' 10 return render_template('login.html') 11 12 @app.route('/test',methods=['POST']) 13 def success(): 14 if request.method == 'POST': 15 email = request.form['email'] 16 return render_template('success.html',email=email) 17 else: 18 pass 19 20 if __name__ == '__main__': 21 app.run( 22 host='0.0.0.0', 23 port=7777, 24 debug=True 25 )
三、在templates文件夹下创建login.html文件和success.html文件
login.html代码如下:
1 <<!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset = utf-8> 5 <title>flask登陆</title> 6 </head> 7 <body> 8 9 <h1>Welcome to the Flask web</h1> 10 <form action='/test' method='post'> 11 <input type='text' name='email' placeholder='Enter Email Address'> 12 <br/><br/> 13 <input type='password' name='pass' placeholder='Passwork'> 14 <input type='submit' value='Submit' name='ok'> 15 16 </form> 17 18 </body> 19 </html>
success.html代码如下:
1 <<!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset = utf-8> 5 <title>登陆成功</title> 6 </head> 7 <body> 8 9 <center><h2>SUCCESSFULLY WITH YOUR EMAIL: {{email}}</h2></center> 10 11 12 </body> 13 </html>
四、在cmd命令行下,切换到test文件夹下运行python2 main.py,
在浏览器网址输入框内输入127.0.0.1:7777/login则转到登陆页面,
在登陆页面输入email和password点击submit则转到登陆成功页面。