Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

1.8K
Visualizações
How to send data in Flask to another page?

I'm using Flask to make a tickets booking app. But for now I'm little confused on how to send data from one page to another page, like this snippet of code:

@app.route('/index', methods = ['GET', 'POST'])
def index():
    if request.method == 'GET':
        date = request.form['date']
        return redirect(url_for('main.booking', date=date))
    return render_template('main/index.html')


@app.route('/booking')
def booking():
    return render_template('main/booking.html')

The date variable is a request from a form, and for now I want to send the date data to booking function. What is term for that purpose..?

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

Passing data is possible for get request from one route to another.

You are almost there to get the submitted date value in booking route.

app.py:

from flask import Flask, render_template, request, jsonify, url_for, redirect

app = Flask(__name__)

@app.route('/', methods = ['GET', 'POST'])
def index():
    if request.method == 'POST':
        date = request.form.get('date')
        return redirect(url_for('booking', date=date))
    return render_template('main/index.html')


@app.route('/booking')
def booking():
    date = request.args.get('date', None)
    return render_template('main/booking.html', date=date)    

if __name__ == '__main__':
    app.run(debug=True)

main/index.html:

<html>
  <head></head>
  <body>
    <h3>Home page</h3>
    <form action="/" method="post">
      <label for="date">Date: </label>
      <input type="date" id="date" name="date">
      <input type="submit" value="Submit">
    </form>
  </body>
</html>

main/booking.html:

<html>
  <head></head>
  <body>
    <h3>Booking page</h3>
    <p>
      Seleted date: {{ date }}
    </p>
  </body>
</html>

Output:

Home route with a form to submit the date

home route

Getting the date in booking route

getting the date in booking route

Disadvantages:

  • The values (e.g.: date) are passed as URL parameters from one route to another.
  • Anyone with a get request can access the second part (e.g. booking route).

Alternatives:

  • Use session storage as @VillageMonkey suggested.
  • Use Ajax to facilitate multi part forms.
over 4 years ago · Santiago Trujillo Relatório

0

You can also use flask session to send data from one page to another page.

 from flask import Flask, render_template, request, jsonify, url_for, redirect, 
 session

 app = Flask(__name__)

 @app.route('/', methods = ['GET', 'POST'])
 def index():
    if request.method == 'POST':
        date = request.form.get('date')
        session["date"] = date
        return redirect(url_for('booking', date=date))
    return render_template('main/index.html')


 @app.route('/booking')
 def booking():
    date = session.get("date")
    return render_template('main/booking.html', date=date)    

if __name__ == '__main__':
    app.run(debug=True)
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda