from flask import Flask, render_template, request, redirect, url_for, session
from flask_mysqldb import MySQL
from cryptography.fernet import Fernet
import MySQLdb.cursors
import bcrypt
app = Flask(__name__)
app.secret_key = 'your secret key'
# Enter your database connection details below
app.config['MYSQL_HOST'] = 'host'
app.config['MYSQL_USER'] = 'root'
app.config['MYSQL_PASSWORD'] = 'password'
app.config['MYSQL_DB'] = 'pythonlogin'
mysql = MySQL(app)
@app.route('', methods=['GET', 'POST'])
def password_reset():
msg = 'asd'
# Check if "username", "password" and "email" POST requests exist (user submitted form)
if request.method == 'POST' and 'username' in request.form and 'password' in request.form and 'new_password' in request.form and 'email' in request.form:
# Create variables for easy access
username = request.form['username']
password = request.form['password']
new_password = request.form['new_password']
email = request.form['email']
#check if the account exist in the database
cursor = mysql.connection.cursor(MySQLdb.cursors.DictCursor)
cursor.execute( "SELECT * FROM accounts WHERE username = %s", (username,))
account = cursor.fetchall()
#if the account exist in database
#the code goes in here
if account:
if account['username'] == session['username']:
salt = bcrypt.gensalt(rounds=16)
hash_password = bcrypt.hashpw(new_password.encode(), salt)
sql = "UPDATE accounts SET password = %s WHERE username = %s "
val = (hash_password, username)
cursor.execute(sql, val)
mysql.connection.commit()
msg = "You have successfully changed the password! "
elif request.method == 'POST':
# Form is empty... (no POST data)
return 'Please fill out the form!'
# Show registration form with message (if any)
return render_template('password_reset.html', msg=msg)
嗨,我正在使用 SQL 连接来执行此操作,我正在尝试重置用户的密码,用户在其中输入用户名、旧密码、新密码和电子邮件。然后我尝试根据用户在表单中输入的内容获取用户信息。
例如用户在表单中输入'john',并且该表单在那里检查数据库中是否有任何用户名'john',然后如果数据库中有'john',我的python代码应该替换数据库中的旧密码和用户在表单中输入的新密码
我所做的代码无法将数据库中的用户名与用户在表单中输入的内容进行比较。请帮忙