一个最基本的登陆注册方式
login.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>用户登录</h1>
<form method="post">
<input type="text" name="user">
<input type="text" name="pwd">
<input type="submit" value="登陆">{{error}}
</form>
</body>
</html>
index.py
# render_template是模板渲染,例如在开发flask项目中,我们会有一个templates文件夹,里面存放一些html文件,
# 这些文件会在视图函数中被渲染,此时就会用到render_template包
from flask import Flask, render_template, request, redirect
app = Flask(__name__)
@app.route('/login', methods=['GET', 'POST']) # @app.route()是thedecorator是装饰器功能
def index():
if request.method == 'GET':
return render_template('login.html')
else:
user = request.form.get('user')
pwd = request.form.get('pwd')
if user == 'xiaoyuyu' and pwd == 'woaini':
return redirect('https://blog.csdn.net/qq_42192672')
else:
return render_template('login.html', error='wrong usernmae or password')
if __name__ == '__main__':
app.run()
都是flask的基础语法,比较难理解的就是修饰器吧,但是直接拿来用还算OK
看一下效果
如果输入错误,会报错;用户名密码正确会跳转到博客首页
很简单吧,嘻嘻