如何在python中的不同路径中将值从def传递到def
我想从def:a()到def:b()获取参数值并返回html文件。如何在python flask中的不同路径中将值从def传递到def如何在python中的不同路径中将值从def传递到def,python,flask,Python,Flask,我想从def:a()到def:b()获取参数值并返回html文件。如何在python flask中的不同路径中将值从def传递到def @app.route('/a',methods=['GET','POST']) def a(): if request.method == 'POST': text_a = request.form.get('text') return render_template('index.html') 和 @app.route('/b'
@app.route('/a',methods=['GET','POST'])
def a():
if request.method == 'POST':
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 = text_a )
return render_template('index.html' )
PS>在路径/a中,我想输入文本并提交,然后获取/a到/b的值
HTML文件
<form method="POST" action= "/a" >
<input type="text" name="text">
<input class="btn btn-primary" type="submit" value="submit">
</form>
{{text}}
{{text}}
谢谢你的帮助
@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>
{{text}}
博览群书
您可以使用Flask存储从一个请求到下一个请求的信息
@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' )
谢谢你帮助我。我做到了谢谢你的帮助我做到了