Home >Backend Development >Python Tutorial >How to Effectively Pass Variables Between Flask Pages?

How to Effectively Pass Variables Between Flask Pages?

Susan Sarandon
Susan SarandonOriginal
2024-12-18 20:32:10232browse

How to Effectively Pass Variables Between Flask Pages?

Passing Variables Between Flask Pages

When navigating between routes in a Flask application, there may be a need to pass variables from one page to another for reuse. This article explores the different ways to accomplish this task, providing comprehensive solutions for both server-side and client-side scenarios.

Server-Side Techniques:

  1. Session:

    • For private data not visible to the user, sessions offer a secure way to store temporary information server-side.
    • The session behaves like a dictionary, allowing storage of any JSON-serializable data.
    • Example:

      # Page A
      session['my_var'] = 'my_value'
      return redirect(url_for('b'))
      
      # Page B
      # ...
      my_var = session.get('my_var', None)

Client-Side Techniques:

  1. Query Parameters:

    • Pass value from a template using query parameters, which are appended to the URL.
    • The retrieved value remains hidden to the end-user.
    • Example:

      <!-- Page A -->
      <a href="{{ url_for('b', my_var='my_value') }}">Send my_value</a>
      
      <!-- Page B -->
      # ...
      my_var = request.args.get('my_var', None)

The above is the detailed content of How to Effectively Pass Variables Between Flask Pages?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn