from flask import Flask, render_template, request
app = Flask(__name__)
# 用户注册
@app.route('/register', methods=['GET', "POST"]) #methods=['GET', "POST"] 两个请求都支持
def register():
if request.method == "GET":
return render_template('register.html')
else:
# 1.接收用户通过POST形式发送过来的数据
# print(request.form)
user = request.form.get("user")
pwd = request.form.get("pwd")
gender = request.form.get("gender")
hobby_list = request.form.getlist("hobby")
city = request.form.get("city")
skill_list = request.form.getlist("skill")
more = request.form.get("more")
# 将用户信息写入到文件中实现注册,写入到excel中实现注册,写入数据库中实现注册
print(user, pwd, gender, hobby_list, city, skill_list, more)
# 2.给用户再返回结果
return "注册成功"
# 用户登录
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == "GET":
return render_template('login.html')
else:
# print(request.form)
user = request.form.get("username")
pwd = request.form.get("password")
print(user, pwd)
return "登录成功"
# 公司首页
@app.route('/index', methods=['GET'])
def index():
return render_template('index.html')
if __name__ == '__main__':
app.run()
login.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>用户登录</h1>
<form method="post" action="/login">
用户名:<input type="text" name="username">
密码:<input type="password" name="password">
<input type="submit" value="提交">
</form>
</body>
</html>
register.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>用户注册</h1>
<form method="post" action="/register">
<div>
用户名:<input type="text" name="user">
</div>
<div>
密码:<input type="password" name="pwd">
</div>
<div>
性别:
<input type="radio" name="gender" value="1">男
<input type="radio" name="gender" value="2">女
</div>
<div>
爱好:
<input type="checkbox" name="hobby" value="10">篮球
<input type="checkbox" name="hobby" value="20">足球
<input type="checkbox" name="hobby" value="30">乒乓球
<input type="checkbox" name="hobby" value="40">棒球
</div>
<div>
城市:
<select name="city" id="">
<option value="bj">北京</option>
<option value="sh">上海</option>
<option value="sz">深圳</option>
</select>
</div>
<div>
擅长的领域:
<select name="skill" id="" multiple>
<option value="100">吃饭</option>
<option value="101">睡觉</option>
<option value="102">打球</option>
</select>
</div>
<div><textarea name="more" id="" cols="30" rows="10"></textarea></div>
<input type="submit" value="submit按钮">
</form>
</body>
</html>