0
votes

I've created a Python Flask site with a login form and a signup form. Both of these are working and when a user signs up, their email, name and password (sha256 hashed) are stored in a sqlite database. 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.

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:

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.

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.

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??

1
Please share directory structureIshan Joshi
Are you using Virtualenv? Also do you have a requirements.txt or requirements.lock file. According to my experience, PyCharm creates a virtualenv by default. Please open the Pycharm terminal and then run the command pip install flask-change-passwordIshan Joshi
check if package is installed in current path/env and for further this link helps youCodenewbie
I opened terminal from within Pycharm within my path/env and typed pip install flask-change-password and received a lot of requirements already satisfied messages.just1han85

1 Answers

0
votes

I ended up fixing this issue by removing flask from the default instance of pip3. I also removed flask-security from the default instance of pip3. I then reinstalled each of these modules in the venv and everything worked.