帮我写一个python Flask 用户登陆页面
时间: 2023-03-25 20:03:53 浏览: 99
好的,我可以帮您编写一个 Python Flask 用户登录页面。您需要安装 Flask 和 Flask-WTF 扩展程序。以下是示例代码:
```
from flask import Flask, render_template, redirect, url_for, flash
from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField, SubmitField
from wtforms.validators import DataRequired, Length, Email
app = Flask(__name__)
app.config['SECRET_KEY'] = 'your_secret_key_here'
class LoginForm(FlaskForm):
email = StringField('Email', validators=[DataRequired(), Email()])
password = PasswordField('Password', validators=[DataRequired(), Length(min=6)])
submit = SubmitField('Log In')
@app.route('/', methods=['GET', 'POST'])
def login():
form = LoginForm()
if form.validate_on_submit():
# Check if email and password are correct
if form.email.data == 'your_email_here' and form.password.data == 'your_password_here':
flash('You have been logged in!', 'success')
return redirect(url_for('home'))
else:
flash('Login unsuccessful. Please check email and password.', 'danger')
return render_template('login.html', form=form)
@app.route('/home')
def home():
return '<h1>You are logged in!</h1>'
if __name__ == '__main__':
app.run(debug=True)
```
请注意,您需要将 `your_secret_key_here`、`your_email_here` 和 `your_password_here` 替换为您自己的值。此外,您需要创建一个名为 `login.html` 的模板文件,以呈现登录表单。
阅读全文