Python 如何使用Flask在HTML页面之间传递变量

Python 如何使用Flask在HTML页面之间传递变量,python,html,flask,Python,Html,Flask,我刚开始使用Flask,我只是尝试在两个网页之间传递一个变量。第一个是接受数字的简单表单,第二个页面仅显示输入的内容 表单页面的HTML: <!doctype html> <html> <body> <form action ="{{ url_for('return_form', glon="glon") }}" method="post"> Galactic Longitude: <input type="t

我刚开始使用Flask,我只是尝试在两个网页之间传递一个变量。第一个是接受数字的简单表单,第二个页面仅显示输入的内容

表单页面的HTML:

<!doctype html>
<html>
<body>
    <form action ="{{ url_for('return_form', glon="glon") }}" method="post">
            Galactic Longitude: <input type="text" name="glon">
        <button type="submit">Submit</button>
    </form>
</body>
</html> 

银河经度:
提交
显示页面的HTML:

<!doctype html>
<body>

<p> {{ glon }} </p>

</body>
</html>

{{glon}}

Flask脚本当前如下所示:

from flask import Flask
from flask import render_template, url_for, request, redirect
app = Flask(__name__)

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

@app.route('/form/', methods = ['GET', 'POST'])
def form():
    if request.method == 'POST':
        glon = request.form['glon']
        #glat = request.form['glat']

        return redirect(url_for('return_form', glon=glon))

    return render_template('form.html')

@app.route('/return_form/<glon>', methods = ['GET', 'POST'])
def return_form(glon):
    return render_template('return_form.html', glon=glon)

if __name__ == '__main__':
    app.run()
从烧瓶导入烧瓶
从flask导入呈现模板、url、请求、重定向
app=烧瓶(名称)
@应用程序路径(“/”)
def index():
返回渲染模板('index.html')
@app.route('/form/',methods=['GET','POST'])
def form():
如果request.method==“POST”:
glon=request.form['glon']
#glat=request.form['glat']
返回重定向(url_for('return_form',glon=glon))
返回呈现模板('form.html')
@app.route('/return\u form/',methods=['GET','POST'])
def退货单(glon):
返回呈现模板('return\u form.html',glon=glon)
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
app.run()
目前,第二个页面只显示“glon”,而不是传递给表单的数字


我只是想让变量显示在第二页上,并最终在return_form函数中使用它。

所以我没有得到你的方法。下面是我所做的,我稍微修改了代码。希望这能解决你的问题

main.py

from flask import Flask
from flask import render_template, url_for, request, redirect
app = Flask(__name__)

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

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

# @app.route('/return_form/<glon>', methods = ['GET', 'POST'])
# def return_form(glon):
#     return render_template('return_form.html', glon=glon)

if __name__ == '__main__':
    app.run()
从烧瓶导入烧瓶
从flask导入呈现模板、url、请求、重定向
app=烧瓶(名称)
@应用程序路径(“/”)
def index():
返回渲染模板('index.html')
@app.route('/form',methods=['GET',POST'])
def form():
如果request.method==“POST”:
glon=request.form['glon']
返回呈现模板('display.html',glon=glon)
#@app.route('/return\u form/',methods=['GET','POST'])
#def退货单(glon):
#返回呈现模板('return\u form.html',glon=glon)
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
app.run()
index.html

<html>
<body>
    <form action ="{{ url_for('form') }}" method="post">
            Galactic Longitude: <input type="text" name="glon">
        <button type="submit">Submit</button>
    </form>
</body>
</html>

银河经度:
提交
display.html

<!doctype html>
<body>

<p> {{ glon }} </p>

</body>
</html>

{{glon}}


Use HTTP method GET或post尝试了此方法,但也无法使其工作,我不确定在本例中form.html模板是如何呈现的。