重定向后,烧瓶变量将不会传输

时间:2019-02-24 21:28:43

标签: html python-3.x flask

没有重定向,一切正常,但是使用此重定向,似乎没有任何作用。我尝试更新代码,尝试强制执行POST请求,检查大量的SO帖子,但是找不到适合我的解决方案。

使用Python 3.7 烧瓶代码:

SET @val = 0;

UPDATE table_name SET Ranking = (@val:=@val+1) ORDER BY column_name;

HTML代码 索引:

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

app = Flask(__name__)

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

@app.route('/', methods=['POST'])
def num_of_students():
    num = request.form['text']
    return redirect(url_for('test123',num=num))

@app.route('/test123')
def test123():
    return render_template("test.html")



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

测试:

<!doctype html>
<title>Rubric</title>

<label>Welcome to the Autograding Rubric</label>
<p>Please enter the number of students.</p>
<form method="POST">
    <input type="text", name="text">
    <input type="submit">
</form>        
</html>

1 个答案:

答案 0 :(得分:1)

模板没有看到num,因为它没有通过render_template传递。试试

@app.route('/test123')
def test123():
    num = request.form["text"]
    return render_template("test.html", num=num)

更新:我想念您正在重复使用'/'作为路由。 Flask对此不会满意。相反,尝试类似

@app.route('/', methods=['GET', 'POST'])
@def index():
    if request.method == 'GET':
        return render_template("index.html")
    num = request.form['text']
    return redirect(url_for('test123'), num=num)
相关问题