简体   繁体   中英

How to avoid python Flask error on Firefox when reloading page

I have a python flask app with pop up modals on the home page, these modals have forms that send data to a database. After the data is sent I am redirecting to the Home page. I am getting Firefox error "To display this page, Firefox must send information that will repeat any action (such as a search or order confirmation) that was performed earlier." If I click on resend the webpage reloads fine but I would like to avoid the message.

@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)

Unfortunately, this "error" is caused by Firefox itself and wouldn't be easy to get rid of, if possible at all. What causes it is Firefox is warning the user about a POST to the server. So, if you refresh it will resend the POST data, and that's not always a good thing.

The only way I can think of to get rid if it is with a GET redirect. So, once the user inputs the data you'd have to redirect them to another page (or the same page) without any POST.

Edit: I realise I forgot to add some information. This warning dialog isn't an error and it isn't specific to Flask. It's a part of HTTP/s POST requests that Firefox shows when you attempt to refresh a page that sent a POST request.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM