简体   繁体   English

Python Flask 更改密码登录表

[英]Python Flask Change Password Login Form

I've created a Python Flask site with a login form and a signup form.我创建了一个 Python Flask 网站,其中包含登录表单和注册表单。 Both of these are working and when a user signs up, their email, name and password (sha256 hashed) are stored in a sqlite database.这两个都有效,当用户注册时,他们的 email、名称和密码(sha256 哈希)存储在 sqlite 数据库中。 I now need to use the flask_change_password library to create a form that will allow users to change their password and I'm just struggling on this.我现在需要使用 flask_change_password 库来创建一个允许用户更改密码的表单,而我正在努力解决这个问题。

First, I'm using PyCharm and installed flask-change-password for my env but when I add this line from flask_change_password import ChangePassword , I get:首先,我使用 PyCharm 并为我的环境安装了 flask-change-password 但是当我from flask_change_password import ChangePassword添加这一行时,我得到:

from flask_change_password import ChangePassword
ModuleNotFoundError: No module named 'flask_change_password'

I don't understand this because I did install it in my env.我不明白这一点,因为我确实在我的环境中安装了它。 I also tried installing with pip pip install flask-change-password to resolve the error without success.我也尝试安装 pip pip install flask-change-password来解决错误但没有成功。

My second problem is that I don't know where I should implement or how to implement the change_password form or how to change a certain password for a specific user.我的第二个问题是我不知道我应该在哪里实现或如何实现 change_password 表单或如何为特定用户更改某个密码。

This is my auth code for signup and login:这是我的注册和登录授权码:

from flask import Blueprint, render_template, redirect, url_for, request, flash
from flask_change_password import ChangePasswordForm
from flask_login import login_user, login_required, logout_user
from sqlalchemy.sql.functions import current_user
from werkzeug.security import generate_password_hash, check_password_hash
from .models import User
from . import db


auth = Blueprint('auth', __name__)


@auth.route('/login')
def login():
    return render_template('login.html')


@auth.route('/signup')
def signup():
    return render_template('signup.html')


@auth.route('/signup', methods=['POST'])
def signup_post():

    email = request.form.get('email')
    name = request.form.get('name')
    password = request.form.get('password')
    user = User.query.filter_by(email=email).first()  # check to see if user already exists

    if user:  # if a user is found, we want to redirect back to signup page so user can try again
        flash('email address already exists. please login with your email.')
        return redirect(url_for('auth.signup'))

    new_user = User(email=email, name=name, password=generate_password_hash(password, method='sha256'))
    # add the new user to the database
    db.session.add(new_user)
    db.session.commit()

    return redirect(url_for('auth.login'))


@auth.route('/login', methods=['POST'])
def login_post():
    email = request.form.get('email')
    password = request.form.get('password')
    remember = True if request.form.get('remember') else False

    user = User.query.filter_by(email=email).first()

    if not user or not check_password_hash(user.password, password):
        flash('Please check your login details and try again.')
        return redirect(url_for('auth.login')) # if the user doesn't exist or password is wrong, reload the page

    login_user(user, remember=remember)
    return redirect(url_for('main.profile'))


@auth.route('/logout')
@login_required
def logout():
    logout_user()
    return render_template('goodbye.html')

My init code:我的初始化代码:

from flask_sqlalchemy import SQLAlchemy
from flask_change_password import ChangePassword

db = SQLAlchemy()


def create_app():
    app = Flask(__name__)
    app.config['SECRET_KEY'] = 'UMGC-SDEV300-Key'
    app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///db.sqlite'

    # app.secret_key = os.urandom(20)
    # flask_change_password = ChangePassword(min_password_length=10, rules=dict(long_password_override=2))
    # flask_change_password.init_app(app)

    db.init_app(app)

    login_manager = LoginManager()
    login_manager.login_view = 'auth.login'
    login_manager.init_app(app)

    from .models import User

    @login_manager.user_loader
    def load_user(user_id):
        return User.query.get(int(user_id))

    from .auth import auth as auth_blueprint
    app.register_blueprint(auth_blueprint)

    from .main import main as main_blueprint
    app.register_blueprint(main_blueprint)

    return app

As you can see, I'm importing the flask_change_password module ChangePasswordForm but It fails to import??如您所见,我正在导入flask_change_password模块ChangePasswordForm但导入失败?

I ended up fixing this issue by removing flask from the default instance of pip3.我最终通过从 pip3 的默认实例中删除 flask 解决了这个问题。 I also removed flask-security from the default instance of pip3.我还从 pip3 的默认实例中删除了 flask-security。 I then reinstalled each of these modules in the venv and everything worked.然后我在 venv 中重新安装了这些模块中的每一个,一切正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM