Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I see below error even after setting secret_key, unable to figure out what i'm missing here please advice

Error:

RuntimeError: The session is unavailable because no secret key was set. Set the secret_key on the application to something unique and secret.

Code below

config.py

from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_login import LoginManager
#from .models import db


db = SQLAlchemy()


def create_app():
    app = Flask(__name__)
    app.config['secret_key'] = 'x90vvxddx11?<xbf xd3xb2xabx12xb5xa3xee'
    app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://postgres:admin@localhost:5432/WebApp'
    db.init_app(app)

    login_manager = LoginManager()
    login_manager.login_view = 'auth.login'
    login_manager.init_app(app)

    from .models import Person

    @login_manager.user_loader
    def load_user(user_id):
        return Person.query.get(int(user_id))

    from .auth import auth as auth_blueprint
    app.register_blueprint(auth_blueprint)
    return app

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
404 views
Welcome To Ask or Share your Answers For Others

1 Answer

There are a few ways of setting a secret key for Flask application.

Using app.config, the SECRET_KEY part must be uppercase here which is why you're receving an error:

app.config['SECRET_KEY'] = 'my-secret-key'

Using Flask application object:

app.secret_key = 'my-secret-key'

More information can be found in Flask documentation: https://flask.palletsprojects.com/en/1.1.x/api/#flask.Flask.secret_key


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...