用pychram、html、flask编写注册页面输入用户名和密码后查看数据库users中的account表中是否有相同的,如果有则在本页面显示已有该用户,如果没有则写入数据库users的account表中
时间: 2024-05-16 09:16:00 浏览: 15
好的,需要安装一些库,首先是 Flask 和 Flask_SQLAlchemy。
```python
pip install Flask Flask_SQLAlchemy
```
下面是代码实现:
```python
# 引入 Flask 和 Flask_SQLAlchemy
from flask import Flask, render_template, request
from flask_sqlalchemy import SQLAlchemy
# 初始化 Flask 应用
app = Flask(__name__)
# 配置数据库连接
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///users.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
# 初始化数据库连接
db = SQLAlchemy(app)
# 定义 User 数据模型类
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(80), unique=True, nullable=False)
password = db.Column(db.String(80), nullable=False)
def __repr__(self):
return '<User %r>' % self.username
# 定义注册路由
@app.route('/register', methods=['GET', 'POST'])
def register():
if request.method == 'POST':
# 获取表单中的用户名和密码
username = request.form['username']
password = request.form['password']
# 查询数据库中是否已有该用户
user = User.query.filter_by(username=username).first()
# 如果已有该用户,返回错误信息
if user:
return render_template('register.html', message='该用户已存在')
# 如果没有该用户,将用户写入数据库
new_user = User(username=username, password=password)
db.session.add(new_user)
db.session.commit()
# 注册成功,返回成功信息
return render_template('register.html', message='注册成功')
# 默认返回注册页面
return render_template('register.html', message='')
# 运行 Flask 应用
if __name__ == '__main__':
app.run(debug=True)
```
上述代码实现了一个简单的注册页面,用户可以在页面中输入用户名和密码,当用户点击注册按钮时,会将用户名和密码写入数据库中,如果用户名已存在,则会提示该用户已存在的错误信息。
注册页面使用了 Flask 的模板渲染功能,模板文件为 `register.html`:
```html
<!DOCTYPE html>
<html>
<head>
<title>注册页面</title>
</head>
<body>
{% if message %}
<p>{{ message }}</p>
{% endif %}
<form method="POST" action="/register">
<label>用户名:</label>
<input type="text" name="username" required><br>
<label>密码:</label>
<input type="password" name="password" required><br>
<input type="submit" value="注册">
</form>
</body>
</html>
```
在模板中,我们使用了 Jinja2 的模板变量和控制语句,当 `message` 变量不为空时,会显示错误信息。`<form>` 标签中的 `method` 属性指定了提交的 HTTP 方法为 POST,`action` 属性指定了提交的 URL 为 `/register`。
当用户点击注册按钮时,会将表单数据提交到 `/register` 路由中,在路由中,我们首先获取表单中的用户名和密码,然后查询数据库中是否已有该用户,如果已有该用户,则返回错误信息,否则将用户写入数据库中。
最后,我们使用 `render_template` 函数将模板渲染为 HTML 页面,返回给用户。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)