使用python flask框架制作3个html页面,分别为,登录,注册,忘记密码
时间: 2024-05-06 17:20:43 浏览: 7
以下是使用Python Flask框架制作的登录、注册和忘记密码三个网页的示例代码:
## 登录页面(login.html)
```html
<!DOCTYPE html>
<html>
<head>
<title>Login</title>
</head>
<body>
<h1>Login</h1>
<form action="/login" method="POST">
<label for="username">Username:</label>
<input type="text" id="username" name="username" required><br><br>
<label for="password">Password:</label>
<input type="password" id="password" name="password" required><br><br>
<input type="submit" value="Login">
</form>
</body>
</html>
```
## 注册页面(register.html)
```html
<!DOCTYPE html>
<html>
<head>
<title>Register</title>
</head>
<body>
<h1>Register</h1>
<form action="/register" method="POST">
<label for="username">Username:</label>
<input type="text" id="username" name="username" required><br><br>
<label for="password">Password:</label>
<input type="password" id="password" name="password" required><br><br>
<label for="confirm_password">Confirm Password:</label>
<input type="password" id="confirm_password" name="confirm_password" required><br><br>
<input type="submit" value="Register">
</form>
</body>
</html>
```
## 忘记密码页面(forgot_password.html)
```html
<!DOCTYPE html>
<html>
<head>
<title>Forgot Password</title>
</head>
<body>
<h1>Forgot Password</h1>
<form action="/forgot_password" method="POST">
<label for="username">Username:</label>
<input type="text" id="username" name="username" required><br><br>
<label for="email">Email:</label>
<input type="email" id="email" name="email" required><br><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
```
在Python Flask中,可以使用以下代码将这三个网页与相应的路由绑定起来:
```python
from flask import Flask, render_template, request
app = Flask(__name__)
@app.route('/')
def index():
return 'Hello, World!'
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
# process login request
username = request.form['username']
password = request.form['password']
# authenticate user
# ...
return 'Logged in successfully'
else:
return render_template('login.html')
@app.route('/register', methods=['GET', 'POST'])
def register():
if request.method == 'POST':
# process registration request
username = request.form['username']
password = request.form['password']
confirm_password = request.form['confirm_password']
# validate input
# ...
# create new user account
# ...
return 'Registered successfully'
else:
return render_template('register.html')
@app.route('/forgot_password', methods=['GET', 'POST'])
def forgot_password():
if request.method == 'POST':
# process forgot password request
username = request.form['username']
email = request.form['email']
# validate input
# ...
# send password reset email
# ...
return 'Password reset email sent'
else:
return render_template('forgot_password.html')
if __name__ == '__main__':
app.run(debug=True)
```
在这个示例代码中,`render_template`函数用于将HTML文件渲染成网页,`request`对象用于获取客户端发送的请求数据。在登录、注册和忘记密码的POST请求中,可以从`request.form`中获取表单数据。在实际应用中,还需要根据具体情况添加用户认证、表单验证和邮件发送等功能。