from flask import Flask,views,render_template,request
app = Flask(__name__)
app.debug = True
class LoginView(views.MethodView):
def __render(self,error=None):
return render_template('login.html', error=error)
def get(self,):
return self.__render()
def post(self):
username = request.form.get('username')
password = request.form.get('password')
if username == 'xiaowu' and password == '123456':
return 'login success'
return self.__render(error="用户名和密码错误")
def put(self):
pass
def delete(self):
pass
app.add_url_rule('/login/',view_func=LoginView.as_view('login'))
if __name__ == '__main__':
app.run()
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>登录页面</title>
</head>
<body>
<div class="form">
<form action="{{ url_for('login') }}" method="post" enctype="multipart/form-data">
<div class="name">
<input name="username" type="text">
</div>
<div class="password">
<input name="password" type="password">
</div>
<button>登录</button>
</form>
</div>
<div>
{% if error %}
<p>{{ error }}</p>
{% endif %}
</div>
</body>
</html>