简体   繁体   English

Flask:手动保存会话数据

[英]Flask: Saving session data manually

I have a Flask application that uses an ajax request, and this request should give a true/false response. 我有一个使用ajax请求的Flask应用程序,该请求应该给出一个true / false响应。 The user enters an authorization code, and if the authorization code matches what is needed or is already in the session, then the response should be true and the code added to the session and saved. 用户输入授权码,如果授权码与会话中需要的或已经存在的匹配,则响应应该为true,并将该代码添加到会话中并保存。 if it's not, then simply return false 如果不是,则返回false

@app.route('/auth-ajax', methods=['POST'])
def auth(): 
    result_id = request.form.get('result_id', 'true')
    result = load_result(result_id)

    if result:
        auth = result['auth_hash']
        auth_input = request.form.get('auth_input', '')
        if (session.get('auths').get(result_id) != auth and auth_input != auth):
            return 'false'

    #else, save new authorization into session
    session['auths'][result_id] = auth

    # return true
    return 'true'

However, the session isn't saving as I'd hope. 但是,会话并没有像我希望的那样保存。 This is my first Python app, and so I'm learning as I go. 这是我的第一个Python应用程序,因此我正在学习。 From what I understand, I need to create a response with Flask and not just simply output "true" or "false" - creating a response save the session as it doesn't save on modification. 据我了解,我需要使用Flask创建响应,而不仅仅是输出“ true”或“ false”-创建响应可以保存会话,因为它不保存修改内容。 The only response functions I've used is render_template() for views, but I'm not wanting a view but rather a simple true/false (possibly HTTP status responses) to see if authorization was granted. 我使用的唯一响应函数是用于视图的render_template(),但是我不想要视图,而是想要一个简单的true / false(可能是HTTP状态响应)来查看是否已授权。 How would I go about doing this? 我将如何去做呢?

Found the problem. 找到了问题。 I simply has to add session.modified = True . 我只需要添加session.modified = True So simple. 很简单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM