I want to save an ID between requests, using Flask session
cookie, but I'm getting an Internal Server Error
as result, when I perform a request.
I prototyped a simple Flask app for demonstrating my problem:
#!/usr/bin/env python from flask import Flask, session app = Flask(__name__) @app.route('/') def run(): session['tmp'] = 43 return '43' if __name__ == '__main__': app.run()
Why I can't store the session
cookie with the following value when I perform the request?
Step 1 — Using The Flask Debugger. In this step, you'll create an application that has a few errors and run it without debug mode to see how the application responds. Then you'll run it with debug mode on and use the debugger to troubleshoot application errors.
Flask-Session is an extension for Flask that supports Server-side Session to your application. The Session is the time between the client logs in to the server and logs out of the server. The data that is required to be saved in the Session is stored in a temporary directory on the server.
For this, we need to download and import flask. Download the flask through the following commands on CMD. Using app.py as our Python file to manage templates, 404. html be the file we will return in the case of a 404 error and header.
According to Flask sessions documentation:
... What this means is that the user could look at the contents of your cookie but not modify it, unless they know the secret key used for signing.
In order to use sessions you have to set a secret key.
Set secret key. And you should return string, not int.
#!/usr/bin/env python from flask import Flask, session app = Flask(__name__) @app.route('/') def run(): session['tmp'] = 43 return '43' if __name__ == '__main__': app.secret_key = 'A0Zr98j/3yX R~XHH!jmN]LWX/,?RT' app.run()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With