Flask-Login and Heroku issues

2020-03-21 05:14发布

I have a sample web application (flask with flask-login running on heroku) at this URL: http://twittaclone.herokuapp.com.

When I run it on my localhost the login functionality works fine. When I push to heroku it freaks out and does not allow users to login (it does allow user registration). Database modifications are being made.

Why would flask login not work on heroku?

app = Flask(__name__)
mysql = MySQL()

app.config['MYSQL_DATABASE_HOST'] = os.environ['MYSQL_DATABASE_HOST'] if 'MYSQL_DATABASE_HOST' in os.environ else config.MYSQL_DATABASE_HOST
app.config['MYSQL_DATABASE_PORT'] = os.environ['MYSQL_DATABASE_PORT'] if 'MYSQL_DATABASE_PORT' in os.environ else config.MYSQL_DATABASE_PORT
app.config['MYSQL_DATABASE_USER'] = os.environ['MYSQL_DATABASE_USER'] if 'MYSQL_DATABASE_USER' in os.environ else config.MYSQL_DATABASE_USER
app.config['MYSQL_DATABASE_PASSWORD'] = os.environ['MYSQL_DATABASE_PASSWORD'] if 'MYSQL_DATABASE_PASSWORD' in os.environ else config.MYSQL_DATABASE_PASSWORD
app.config['MYSQL_DATABASE_DB'] = os.environ['MYSQL_DATABASE_DB'] if 'MYSQL_DATABASE_DB' in os.environ else config.MYSQL_DATABASE_DB

mysql.init_app(app)

if 'SECRET_KEY' in os.environ: app.config['SECRET_KEY'] = os.environ['SECRET_KEY']
else: app.config['SECRET_KEY'] = os.urandom(24)

def connect_db(): return mysql.connect()


###
# Routing for your application.
###

login_manager = LoginManager()
login_manager.login_view = "login"

@login_manager.user_loader
def load_user(username):
    g.db = connect_db()
    return get_user(username)

login_manager.init_app(app)

@app.route('/')
def home(): return render_template('home.html')

def connect_db(): return mysql.connect()

@app.before_request
def before_request():
    g.user = current_user
    g.db = connect_db()

@app.teardown_request
def tear_down(exception):
    g.db.close()

@app.route('/main/')
@login_required
def main():
    print("in main")
    tweets, user = get_main()
    follower_count, followee_count = get_follower_info(g.user.username)
    return render_template('main.html', user=user, tweets=tweets, followercount =      follower_count, followeecount = followee_count)


@app.route('/login/', methods=['GET', 'POST'])
def login():
    """Logs the user in."""
    if request.method == 'GET':
        if current_user is user_logged_in: logout_user()

    error = None
    if request.method == 'POST':
        user = get_user(request.form['username'])
        if user is None:
            error = 'Invalid username'
        elif not check_password_hash(user.password, request.form['password']):
            error = 'Invalid password'
        else:
            flash('You were logged in')
            login_user(user)
            return redirect(url_for('main'))

    return render_template('login.html', error=error)

1条回答
成全新的幸福
2楼-- · 2020-03-21 06:10

I'm pretty sure that Heroku uses PostgreSQL. Try referring to their documents https://devcenter.heroku.com/articles/heroku-postgresql

查看更多
登录 后发表回答