K
K
kunjut192020-07-31 01:33:06
Flask
kunjut19, 2020-07-31 01:33:06

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)


routes.py
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)


When I drop flask_session no such error appears

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
iddqda, 2020-07-31
@iddqda

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 question

Ask a Question

731 491 924 answers to any question