Flask - 将反馈数据存储到服务器

我使用烧瓶应用程序作为服务器.我只是返回一个用户交易.但是,如果交易错误,我需要允许用户向服务器提供反馈.

I am using a flask application as the server. I simply return a users transactions. However, I need to allow the user to give the server feedback if a transaction was wrong.

from flask import Flask, render_template, request
import pandas as pd
pd.set_option('display.max_colwidth', -1)

app = Flask(__name__)

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

@app.route("/submit", methods=['GET','POST'])
def submit_table():
    if request.method == 'POST':
        if request.form['submit'] == 'Get Started!':
            last_month_txn = pd.read_csv("./Predicted_Reoccuring_Payments.csv")
            last_month_txn = last_month_txn[["processing_dt", "narrative","transaction_amt", "trans_type_desc"]]
if __name__ == '__main__':
   app.run(port=5050, threaded=True)

submit_table 函数将读取 pandas 数据帧并将其发送到页面.这在用户登录时有效.我没有包含登录表单.我有一个引导模式,如果有人想对表中的条目提出异议,就会出现该模式.

The submit_table function will read in a pandas dataframe and send it to the page. This works when a user logs in. I have not included the log in form. I have a bootstrap modal which is brought up if a person wants to dispute an entry in the table.

 <form  method = "POST" action="submit">
  <div class="modal fade modal-admin" id="myModal" role="dialog">
    <div class="modal-dialog modal-lg">
      <!-- Modal content-->
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal">&times;</button>
          <h3 class="modal-title" name="feedback">Submit Feedback</h3>
        </div>
        <div class="modal-body">
            <h4>Please provide feedback about this transaction</h4>

          <div class="form-group" style="padding:20px;">
            <label for="text_area">Additional information</label>
            <textarea class="form-control mywidth" id="text_area" rows="3" name="text_area"></textarea>
          </div>

        <div class="modal-footer">
            <button type="button" class="btn btn-primary" id="submit_modal" data-dismiss="modal">Submit</button>
          <button type="button" class="btn btn-primary" data-dismiss="modal">Close</button>
        </div>
      </div>
    </div>
  </div>

我需要将这些数据(特别是文本区域的内容)记录到服务器上的某个文件中.我不希望网页在点击提交时重定向/刷新.烧瓶有没有办法在不影响 url/web 地址的情况下获取此表单的内容?

I need to log this data, specifically the contents of the textarea, into some file on the server. I don't want the webpage to redirect/refresh when they hit submit. Is there a way for flask to get the contents of this form without affecting the url/web address?

推荐答案

我认为你必须在 javascript 中使用 Ajax 请求.您可以使用 Jquery(这很简单)或经典的 javascript 来完成.

I think you have to use the Ajax Request in javascript. You can do it with Jquery (which is pretty easy) or in classic javascript.

您可以在 google 上找到一些文档!

Edit : You can find some docs on google !

小例子:

$.ajax({
  type     : 'POST',
  url      : '/path_url',
  dataType : "json",
  contentType: 'application/json;charset=UTF-8',
  data     : JSON.stringify(dict_var),
  success : function (result) {
    console.log(result)
  },
  error : function (error) {
    console.log(error);
  }
});

使用字典 dict_var,您只需从表单中恢复数据(非常简单)并通过字典发送.

With dict_var which is a dictionnary, you just need to recover data from your form (pretty simple) and send it through dictionnary.

而在烧瓶中,你只需要在你的路由中读取数据:

And in flask, you just need to read data in your route:

data = json.loads(request.data)
tmp = data["key"]

和平

相关文章