Answer the question
In order to leave comments, you need to log in
Why does "The session is unavailable because no secret key was set" appear?
There is such an error when trying to log in
__init__.py
from flask import Flask
...
from flask_session import Session
app = Flask(__name__)
app.config['SECRET_KEY'] = 'gfgfgghghgfhgfhgfhgfhfgghghghghghg'
Session(app)
from flask import render_template, session, url_for, redirect, request, Blueprint
...
from mywebsite.users.forms import (LoginForm, RegisterForm)
from mywebsite import db, bcrypt, app
users = Blueprint('users', "__name__")
@users.route('/login', methods=['GET', 'POST'])
def login():
loginForm = LoginForm()
if request.method == 'POST':
# очищается, если уже хранит пользователя
session.pop('user_id', None)
Answer the question
In order to leave comments, you need to log in
Nicoda didn't use session.
I usually use flask_login,
but the logic suggests that session.pop() should be done not for login, but for logout
, even the comment hints that the user is being taken out here, and they are not
inaccurately copy-pasted
.
if request.method == 'POST':
session['user_id'] = request.form['username']
return redirect(url_for('index'))
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question