无法在帖子请求下在Flask中重定向

时间:2018-04-01 01:47:34

标签: redirect flask

我正在开发一个关于Flask写的食物的小网站。但是,在用户在搜索栏中键入内容然后单击搜索按钮后,我无法将用户重定向到新页面。

@app.route('/', methods = ['GET','POST'])
def home():

    if request.method == "POST":
        search      = request.form['search']
        print(search)

        sql_search  = 'SELECT Food.name,Food.area,Food.price,Food.description,Food.available_amount,Food.score,AVG_PRICE,AVG_SCORE,User.selling_score FROM Food LEFT JOIN(SELECT Food.name, Food.area ,AVG(Food.price) AS AVG_PRICE,AVG(Food.score) AS AVG_SCORE FROM Food GROUP BY Food.name, Food.area) NEW_FOOD ON Food.name = NEW_FOOD.name AND Food.area = NEW_FOOD.area LEFT JOIN User ON Food.maker_id = User.id WHERE Food.name LIKE "%s" ORDER BY Food.price,Food.score DESC,User.selling_score DESC' % search

        search_list = db.session.execute(sql_search).fetchall()
        search_l    = process_dict(search_list, attr_search)
        print(search_l)
        return redirect(url_for('test'), code = 303)

    return render_template('home.html')

这是我在主页上搜索“咖喱”后终端的输出。

curry
[{'name': 'curry', 'area': 61801, 'price': 6.0, 'description': 'nice', 'available_amount': 1, 'score': 5.0, 'AVG_PRICE': 6.0, 'AVG_SCORE': 5.0, 'seller_score': 0.0}, {'name': 'curry', 'area': 61820, 'price': 8.0, 'description': 'ok', 'available_amount': 2, 'score': 5.0, 'AVG_PRICE': 8.666666666666666, 'AVG_SCORE': 4.0, 'seller_score': 0.0}, {'name': 'curry', 'area': 61820, 'price': 8.0, 'description': 'excellent', 'available_amount': 2, 'score': 4.0, 'AVG_PRICE': 8.666666666666666, 'AVG_SCORE': 4.0, 'seller_score': 0.0}, {'name': 'curry', 'area': 61820, 'price': 10.0, 'description': 'excellent', 'available_amount': 1, 'score': 3.0, 'AVG_PRICE': 8.666666666666666, 'AVG_SCORE': 4.0, 'seller_score': 0.0}]
10.193.20.119 - - [31/Mar/2018 20:40:41] "POST / HTTP/1.1" 303 -
10.193.20.119 - - [31/Mar/2018 20:40:41] "GET /test HTTP/1.1" 200 

虽然终端显示它向我想要重定向到的页面发送get请求,但我的页面实际上没有变化。有没有人对我的代码出了什么问题有一些想法?

这是我简单测试页的代码:

@app.route('/test')
# @login_required
def test():
    return "hello"

0 个答案:

没有答案