-1

I want get value of parameter form def:a() to def:b() and return to html file. How can I pass value from def to def in differrent path in python flask?

@app.route('/a',methods=['GET','POST'])
def a():
    if request.method == 'POST':
       text_a = request.form.get('text')
    return render_template('index.html')

AND

@app.route('/b',methods=['GET','POST'])
def b():
    if request.method == 'POST':
       return render_template('index.html',text = text_a )
    return render_template('index.html' )

PS> in path /a I want to input text and submit then get value of /a to /b

HTML file

 <form method="POST" action= "/a" >
  <input type="text" name="text">
<input class="btn btn-primary" type="submit"  value="submit">
 </form>

 {{text}}

Thank you for help.

2 Answers2

0
@app.route('/a') #localhost/a
def a():
    return render_template('index.html')


@app.route('/b',methods=['GET','POST'])
def b():
    if request.method == 'POST':
        text_a = request.form['text']
        return render_template('index.html',text = text_a )
    return render_template('index.html' )    

index.html

<html>
<head>
</head>
<body>
     <form method="POST" action= "{{ url_for('b') }}" >
  <input type="text" name="text">
<input class="btn btn-primary" type="submit"  value="submit">
 </form>

 {{text}}
</body>
</html>

Good Read https://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-world

iamklaus
  • 3,720
  • 2
  • 12
  • 21
0

You can use Flask sessions to store information from one request to the next.

@app.route('/a',methods=['GET','POST'])
def a():
    if request.method == 'POST':
       session['text_a'] = request.form.get('text')
    return render_template('index.html')

@app.route('/b',methods=['GET','POST'])
def b():
    if request.method == 'POST':
       return render_template('index.html',text=session['text_a'] )
    return render_template('index.html' )
simanacci
  • 2,197
  • 3
  • 26
  • 35