立即学习:https://edu.csdn.net/course/play/27093/354196?utm_source=blogtoedu
flask基于方法的类视图
MethodView
如:
1、app.py文件
from flask import Flask, render_template, request, views app = Flask(__name__) @app.route('/') def hello_world(): return render_template('index.html') class MailLoginView(views.MethodView): def get(self): return "" def post(self): mailname = request.form.get('mailname') pwd = request.form.get('pwd') if mailname == 'dabing' and pwd == '123': return '成功,登陆成功' else: return '用户名或密码失败,请检查' app.add_url_rule('/mailLoginView', view_func=MailLoginView.as_view('mailLoginView')) if __name__ == '__main__': app.run()
2、index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <form action='mailLoginView' method="post"> <input name="mailname" type="text" placeholder="请输入账号"> <br> <input name="pwd" type="password" placeholder="请输入密码"><br> <input type="submit" value="登录"><br> </form> </body> </html>
组成
成功登陆提示
登陆失败提示