Flask 注册页面 / 两个函数,俩个url,都是用作注册,通过method请求方式不同,整合到一个函数里面

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 "注册成功"

if __name__ == '__main__':
app.run()


<!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>
posted @ 2023-01-12 17:56  严永富  阅读(17)  评论(0)    收藏  举报