5

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)
4

2 回答 2

0

许多年后,但这解决了您的问题:Heroku gunicorn flask login is not working proper

更新的Procfile:

网页:gunicorn app:app --preload

gunicorn 预加载文档:https ://docs.gunicorn.org/en/stable/settings.html

于 2022-01-18T01:00:45.123 回答
-1

我很确定 Heroku 使用 PostgreSQL。尝试参考他们的文档https://devcenter.heroku.com/articles/heroku-postgresql

于 2014-09-12T00:42:40.473 回答