简体   繁体   中英

Flask: Saving session data manually

I have a Flask application that uses an ajax request, and this request should give a true/false response. 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. if it's not, then simply return 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. 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. 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. How would I go about doing this?

Found the problem. I simply has to add session.modified = True . So simple.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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