If you want to pass some python value that the user should not see or control, you can use a session:
@app.route('/a') def a(): session['my_var'] = 'my_value' return redirect(url_for('b')) @app.route('/b') def b(): my_var = session.get('my_var', None) return my_var
A session behaves like a dict and is serialized in JSON. This way you can put everything that JSON serializes into the session. However, note that most browsers do not support session cookies larger than ~ 4000 bytes.
Avoid placing large amounts of data in a session, as they must be sent to and from the client with each request. For large amounts of data, use a database or other data warehouse. See Are global variables safe in a thread How can I share data between requests? and store big data or a service connection in a Flask session .
If you want to pass the value from the template to the URL, you can use the query parameter:
<a href="{{ url_for('b', my_var='my_value') }}">Send my_value</a>
will give out the url:
/b?my_var=my_value
which can be read from b:
@app.route('/b') def b(): my_var = request.args.get('my_var', None)
davidism
source share