Flask(12)-视图-基于调度类视图及其使用
基于调度
method方法有:POST、GET、DELETE、PUT
# app
from flask import Flask, render_template, url_for
from flask import views, request
app = Flask(__name__)
# 登录视图,继承MethodView类
class LoginView(views.MethodView):
# get方式
def get(self, error=None):
return render_template("login.html", error=error)
# post方式
def post(self):
# 获取post提交的username和password
username = request.form.get("username")
password = request.form.get("password")
# 用户密码判断
if username == "asd" and password == "hahaha":
return "登录成功!"
else:
return self.get(error="登录失败")
# 注册url
app.add_url_rule("/login/", view_func=LoginView.as_view("login"))
if __name__ == '__main__':
app.run(debug=True)
<!--HTML login-->
<!DOCTYPE html>
<html lang="zh-en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Title</title>
</head>
<body>
<h1>登录页</h1>
<!--form提交,post方式-->
<form action="" method="post">
<table>
<tbody>
<tr>
<td>用户名:</td>
<td><input type="text" name="username"></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="password"></td>
</tr>
<tr>
<td><input type="submit" value="提交"></td>
</tr>
</tbody>
{% if error %}
<p style="color:red;">{
{ error }}</p>
{% endif %}
</table>
</form>
</body>
</html>
正文到此结束
评论
登录后才能发表评论 登录/注册
0评论