瓶,登录和Heroku的问题(Flask-Login and Heroku issues)

2019-07-04 05:25发布

我有一个样本Web应用程序(量瓶中,用在Heroku上运行烧瓶登录)在这个网址: http://twittaclone.herokuapp.com 。

当我在我的本地运行它的登录功能工作正常。 当我推到它的Heroku怪胎,不会允许用户登录(它允许用户注册)。 数据库修改正在取得进展。

为什么瓶未登入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)

Answer 1:

我敢肯定,Heroku的使用PostgreSQL的。 尽量指他们的文档https://devcenter.heroku.com/articles/heroku-postgresql



文章来源: Flask-Login and Heroku issues