会话不会在flask-session中持续

时间:2019-08-13 20:47:14

标签: python flask flask-session

我有一个reactJS Web应用程序作为前端,我正在请求在Flask服务器中路由“ / addData”并设置一个名为“ FileName”的状态变量。

session['fileName'] = 'smthing'

然后紧接着在这一行之后,我将会话变量打印为:

print(session['fileName'])

并打印了分配的专有名称。

然后我在烧瓶服务器中将另一个路由称为“ / visualize”。当我尝试使用以下方法访问此路由内的会话变量时:

print(session.get('fileName', 'not set'))

它显示“未设置”。

注意:我正在使用Flask会话(服务器端会话)

下面显示的是我的 init .py文件

from flask import Flask
from flask_socketio import SocketIO
from flask_sqlalchemy import SQLAlchemy
from flask_cors import CORS, cross_origin
from flask_session import Session
import os

socketio = SocketIO()
db = SQLAlchemy()
sess = Session()

def create_app(debug=False):

    app = Flask(__name__)
    app.debug = debug
    app.config['SESSION_TYPE'] = 'filesystem'
    app.config['SECRET_KEY'] = 'secret'
    app.config['USE_PERMANENT_SESSION'] = True
    # app.config['SECRET_KEY'] = os.urandom(25)
    app.config['SQLALCHEMY_DATABASE_URI'] = "mysql://root:pass@localhost/tensormap"
    app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False

    from .resources import main
    app.register_blueprint(main)

    socketio.init_app(app)
    db.init_app(app)
    sess.init_app(app)
    CORS(app, expose_headers='Authorization', support_credentials=True)
    return app

下面显示了我的2条路线:

from flask import jsonify, redirect, url_for, render_template, request, session
from . import main
from .. import db
from .database_models.data_preproc import dataset
import os
import json

@main.route('/addData', methods=['POST'])
def addData():

    file = request.files['file']
    datasetFile = file.filename

    dirPath = os.path.abspath(os.path.join(os.path.dirname( __file__ ), '.', 'dataset'))
    path = '{}{}{}'.format(dirPath, "/", datasetFile)

    splitFileInfo = datasetFile.split(".")
    fileName = splitFileInfo[0]
    fileFormat = splitFileInfo[1]

    if dataset.query.filter_by(fileName=fileName).one_or_none():

        print("**********************************")
        session['fileName'] = splitFileInfo[0]
        print(session.get('fileName', 'not set'))
        return "error"


    else:

        file.save(path)   

        session['fileName'] = splitFileInfo[0]
        print(session['fileName'])

        # writing the file entry in the database.
        data = dataset(fileName, path, fileFormat)
        db.session.add(data)
        db.session.commit()   
        print("**********************************")

        return "file uploaded successfully"

@main.route('/visualizeData', methods=['GET'])
def visualizeData():
    print(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>")
    print(session.get('fileName', 'not set'))
    entry = dataset.query.filter_by(fileName='store').one()
    print(entry.filePath)
    if entry:
        with open(entry.filePath, 'r') as f:
            data = f.readlines()
            default = ""
            data = default.join(data)
            return data
    else:
        return 'None'

我在StackOverflow中尝试了很多东西,但是没有任何效果。我在做什么错了?

0 个答案:

没有答案