如何在重新加载页面时避免Firefox上的python Flask错误

时间:2018-05-25 01:50:38

标签: python post flask get web-development-server

我在主页上有一个带有弹出模式的python烧瓶应用程序,这些模式具有将数据发送到数据库的表单。发送数据后,我将重定向到主页。我收到Firefox错误“要显示此页面,Firefox必须发送信息,这些信息将重复之前执行的任何操作(例如搜索或订单确认)。”如果我点击重新发送网页重新加载正常但我想避免该消息。

@app.route('/home', methods= ['GET', 'POST'])
def index():
    info = get_info()
    data = get_data()
    load_dictionary = get_load()

      if request.method == 'POST':
        name = request.form['id']
        if int(request.form['library_full']) == 0 and request.form['load_number'] == '':
          return redirect(url_for('tks_msg'))
        if int(request.form['library_full']) == 0:
          load_number = request.form['load_number']
        else:
          load_number = request.form['library_full']

        send_load(name= name, load= load_number)
        load_dictionary = get_load()
        info = get_info()
        data = get_data()
        return render_template('names3.html', names= info, data= data, load= load_dictionary)  

    return render_template('names3.html', atr_names= info, data= data, load= load_dictionary)


@app.route('/edit/<string:name>/', methods = ['GET', 'POST'])
def edit_form(name):
    if request.method == 'POST':
       name= name
       comment= request.form['user_comment']
       condition = request.form['condition']
      # load_number= request.form['load_number']
       status = request.form['status']
       send_information(name= name, status= status, condition = condition, comments= comment)

       return redirect(url_for('index'))
    else:
       return render_template('edit_shaker.html', name= name)


@app.route('/thankyou', methods= ['GET'])
def tks_msg():
    return render_template('thankyou.html')



if __name__ == '__main__':
    app.run(host= '0.0.0.0', port= 2345, debug= True)

2 个答案:

答案 0 :(得分:1)

不幸的是,这个“错误”是由Firefox本身造成的,如果可能的话,不容易摆脱。导致它的原因是Firefox警告用户有关服务器的POST。因此,如果你刷新它将重新发送POST数据,这并不总是一件好事。

如果有GET重定向,我能想到摆脱的唯一方法。因此,一旦用户输入数据,您就必须将它们重定向到另一个页面(或同一页面)而不进行任何POST。

编辑: 我意识到我忘了添加一些信息。此警告对话框不是错误,并不是特定于Flask。这是Firefox在您尝试刷新发送POST请求的页面时显示的HTTP / s POST请求的一部分。

答案 1 :(得分:0)

解决方案非常简单,而不是使用渲染模板我只能使用重定向和url_for。这会将我带回更新信息的网址。