Flask SQLALchemy 的使用

Flask-SQLAlchemy 是一个 Flask 扩展,它简化了 Flask 应用中 SQLAlchemy 的使用。SQLAlchemy 是一个强大的 SQL 工具包和对象关系映射(ORM)工具,它支持多种数据库后台。使用 Flask-SQLAlchemy,你可以通过定义 Python 类来创建数据库表,并通过这些类的实例来操作数据库中的数据。

安装 Flask-SQLAlchemy

首先,你需要安装 Flask-SQLAlchemy。这可以通过 pip 完成:

pip install Flask-SQLAlchemy

配置 Flask-SQLAlchemy

在你的 Flask 应用中,你需要配置 Flask-SQLAlchemy。这通常在创建 Flask 应用对象后,配置数据库 URI(统一资源标识符)时完成。

from flask import Flask
from flask_sqlalchemy import SQLAlchemy

app = Flask(
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
首先,需要引入 Flask 和相关的模块: ```python from flask import Flask, render_template, request, redirect, url_for, flash from flask_sqlalchemy import SQLAlchemy from werkzeug.security import generate_password_hash, check_password_hash ``` 然后,需要创建 Flask 应用程序实例,并且设置数据库连接: ```python app = Flask(__name__) app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///users.db' app.config['SECRET_KEY'] = 'your_secret_key' db = SQLAlchemy(app) ``` 接下来,定义用户数据模型: ```python class User(db.Model): id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(50), unique=True) password = db.Column(db.String(100)) ``` 然后,定义修改密码的路由和视图函数: ```python @app.route('/change_password', methods=['GET', 'POST']) def change_password(): if request.method == 'POST': current_password = request.form['current_password'] new_password = request.form['new_password'] confirm_password = request.form['confirm_password'] user = User.query.filter_by(username=session['username']).first() if check_password_hash(user.password, current_password): if new_password == confirm_password: user.password = generate_password_hash(new_password) db.session.commit() flash('Password changed successfully!', 'success') return redirect(url_for('profile')) else: flash('New password and confirm password do not match!', 'error') else: flash('Current password is incorrect!', 'error') return render_template('change_password.html') ``` 最后,在 HTML 模板文件中添加表单: ```html {% extends 'base.html' %} {% block content %} <h1>Change Password</h1> {% with messages = get_flashed_messages() %} {% if messages %} <ul class="messages"> {% for message in messages %} <li class="{{ message[0] }}">{{ message[1] }}</li> {% endfor %} </ul> {% endif %} {% endwith %} <form method="post"> <label for="current_password">Current Password:</label> <input type="password" name="current_password" id="current_password" required> <br> <label for="new_password">New Password:</label> <input type="password" name="new_password" id="new_password" required> <br> <label for="confirm_password">Confirm Password:</label> <input type="password" name="confirm_password" id="confirm_password" required> <br> <input type="submit" value="Change Password"> </form> {% endblock %} ``` 这样就完成了使用 Flask 实现修改密码功能。需要注意的是,密码需要使用哈希函数进行加密,以保护用户的隐私。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Jasonakeke

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值