该会话不可用,因为未设置任何秘密密钥.将应用程序上的secret_key设置为唯一且秘密的内容.烧瓶/Heroku [英] the session is unavailable because no secret key was set. Set the secret_key on the application to something unique and secret. Flask/Heroku

查看:506
本文介绍了该会话不可用,因为未设置任何秘密密钥.将应用程序上的secret_key设置为唯一且秘密的内容.烧瓶/Heroku的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

flask应用程序可以登录并在localhost上进行所有注册.但是,当我将其推送到heroku时,这成为一个问题.它显示了上述错误.这是app.py代码

The flask app can login and register all fine on localhost. But this becomes an issue when i push it to heroku. It shows the above mentioned error. Here's the app.py code

from flask import Flask, render_template, request, redirect, jsonify, url_for, flash
from sqlalchemy import create_engine, asc, desc
from sqlalchemy.orm import sessionmaker
from database_setup import Base, User, BlogPost
from flask import session as login_session
import random
import string
from wtforms import Form, BooleanField, TextField, PasswordField, validators
from passlib.hash import sha256_crypt


app = Flask(__name__)


#Connecting to database
engine = create_engine('sqlite:///travellerdata.db')
Base.metadata.bind = engine

DBSession = sessionmaker(bind=engine)
session = DBSession()

最后以...

if __name__ == "__main__":
    app.secret_key = 'some secret key'
    app.debug = True
    app.run()

推荐答案

很可能在HTTP服务器加载应用程序时,__name__不等于'main'.尝试将app.secret_key = 'some secret key'行移至if块之外.

It's likely that when your HTTP server is loading your application, __name__ is not equal to 'main'. Try moving the line app.secret_key = 'some secret key' outside the if block.

把您的秘密密钥放在源代码中不是一个好主意,因为如果有人得到了它,他们就可以恶意访问您的系统.尝试将其存储在应用程序实例目录(此处的代码段)中的文件中或将其放在环境变量(在此处解释).

It's not a good idea to put your secret key in source code because if anyone gets it they can malevolently gain access to your system. Try storing it in a file in the application's instance directory (snippet here) or putting it in an environment variable (explanation here).

这篇关于该会话不可用,因为未设置任何秘密密钥.将应用程序上的secret_key设置为唯一且秘密的内容.烧瓶/Heroku的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆