简体   繁体   English

在Flask后端上实现POST / REDIRECT / GET

[英]Implementing POST/REDIRECT/GET on flask backend

I'm a relative beginner in web dev and javascript and I've made a flask app that takes in a user form and puts the data in a table. 我是Web开发人员和javascript的相对初学者,我制作了一个flask应用程序,该应用程序采用用户表单并将数据放入表格中。 However, when I reload the template containing the table, the form resubmits. 但是,当我重新加载包含表的模板时,表单将重新提交。 I know that I have to implement a post/redirect/get system and I understand the logic behind it, but in terms of code, I just don't know what I should do. 我知道我必须实现一个post / redirect / get系统,并且我理解其背后的逻辑,但是就代码而言,我只是不知道该怎么做。

#New Order Page
@app.route('/new-cad-order', methods=['GET', 'POST'])
def CAD_order():

    if request.method == 'POST':
        Order_ID = request.form['Order_ID']
        Order_Date = request.form['Order_Date']
        Customer_Name = request.form['Customer_Name']
        Order_Balance_Without_Tax = request.form['Order_Balance_Without_Tax']
        Delivery_Date = request.form['Delivery_Date']
        Final_Order_Balance_With_Tax = request.form['Final_Order_Balance_With_Tax']
        Payment_Date = request.form['Payment_Date']
        Check_Number = request.form['Check_Number']
        Deposit_Date = request.form['Deposit_Date']
        Verification_Date = request.form['Verification_Date']

        key = 'Allow'

        #Processing the data

        return render_template('tracking.html', ID=Order_ID, OrderDate=Order_Date, CustomerName=Customer_Name,
    OrderBalance=Order_Balance_Without_Tax, Downpayment=Downpayment_Balance, Delivery=Order_Delivered, DeliveryDate=Delivery_Date,
    FinalOrderBalance=Final_Order_Balance_With_Tax, RCheck=Revenue_Check, PReceival=Payment_Received, PaymentDate=Payment_Date,
    CheckNum=Check_Number, Deposit=Bank_Deposit, DepositDate=Deposit_Date, Ver=Verification, VerDate=Verification_Date, key=key)

    return render_template('CAD_order.html')

After some code fumbling, I found the answer! 经过一些代码弄乱后,我找到了答案! All I did was change the url of the table template page after registering the new form data. 我所做的就是在注册新表单数据后更改表格模板页面的url。 Now the user can't resubmit the form because the method changes to GET. 现在,由于方法更改为GET,因此用户无法重新提交表单。

<script>
var table = document.getElementById("orders");
if(localStorage.tableData==undefined){localStorage.tableData=table.innerHTML};
table.innerHTML = localStorage.tableData;

//Registering changes based on form data... 

localStorage.tableData=table.innerHTML;
document.location.href = 'http://localhost:5000/tracking';
  };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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