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()
And ends with...
if __name__ == "__main__":
app.secret_key = 'some secret key'
app.debug = True
app.run()
Generate the Secret Key Using Different Ways in Flask and Python. To access a session ID, you need to use an encryption key assigned to the SECRET_KEY variable, so at the time, we set the value of the SECRET_KEY variable as a string is extremely dangerous. This key needs to be randomly generated.
Each Flask web application contains a secret key which used to sign session cookies for protection against cookie data tampering. It's very important that an attacker doesn't know the value of this secret key. Your application is using a weak/known secret key and Acunetix managed to guess this key.
Flask-Session is an extension for Flask that supports Server-side Session to your application. The Session is the time between the client logs in to the server and logs out of the server. The data that is required to be saved in the Session is stored in a temporary directory on the server.
flask runtimeerror: the session is unavailable because no secret key was set. set the secret_key on the application to something unique and secret. runtimeerror: the session is unavailable because no secret key was set. set the secret_key on the application to something unique and secret.
python - RuntimeError: The session is unavailable because no secret key was set. Set the secret_key on the application to something unique and secret - Stack Overflow I am making Flask app.
Code Answer The session is unavailable because no secret key was set. Set the secret_key on the application to something unique and secret. Whatever answers related to “The session is unavailable because no secret key was set. Set the secret_key on the application to something unique and secret.” No application encryption key has been specified.
It states that you must set SESSION_TYPE and only lists options where the session is stored server-side. But that is not [currently] correct. If you succeed in setting a SECRET_KEY then the default session type will store the session in an encrypted cookie (not server side).
I have the same issue when I use flask-login to generate a session ID, it works fine when I directly run it but will output error when I use HTTP server. The original code is like:
if __name__ == "__main__":
app.secret_key = os.urandom(24)
app.run()
Then I moved app.secret_key = os.urandom(24)
out of __name__
and put it under app = Flask(__name__)
like this:
app = Flask(__name__)
app.secret_key = os.urandom(24)
login_manager = flask_login.LoginManager()
login_manager.init_app(app)
And it works fine now.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With