简体   繁体   English

获取HTML表行数据到Flask function

[英]Get HTML table row data to Flask function

I have an HTML table with several columns in the below template.我有一个 HTML 表,下面的模板中有几列。 The last column of the table has a button.表格的最后一列有一个按钮。 The code is as follows.代码如下。

index.html索引.html

<table id="deployment_table" class="table table-striped">
   <thead>
      <tr>
         {% for col in colnames %}
         <th>{{ col }}</th>
         {% endfor %}
      </tr>
   </thead>
   <tbody>
      {% for record in records %}
      <tr>
         {% for col in colnames %}
         <td>{{ record[col] }}</td>
         {% endfor %}
         <td class="td-actions text-right">
            <a href=ct_edit ><input type="submit" name="edit_btn" value="update"rel="tooltip" class="btn btn-info"></a>
            </input>
         </td>
      </tr>
      {% endfor %}
   </tbody>
</table>

When the user click the button of a specific row, I need to pass the value of the first column of the selected row to a Flask function and load another HTML page using this data.当用户单击特定行的按钮时,我需要将所选行的第一列的值传递给 Flask function 并使用此数据加载另一个 HTML 页面。

Below is the flask back end function.下面是 flask 后端 function。

@app.route('/ct_edit')
def ct_edit():
    print('ct_edit')
    # the value of the first column of the selected row => ID
    # do something with the ID value and get a result
    return render_template('ct_edit.html', result=result) 

So far I'm unable to make this work.到目前为止,我无法完成这项工作。 Can I please have any advice for this?请问对此我有什么建议吗?

First of all, note that your input tag closes the anchor tag when it should be the other way around not sure it does affect the code in this example but it's good to keep them in order though.首先,请注意,当您的输入标记关闭锚标记时,它应该是相反的,不确定它是否会影响本示例中的代码,但最好保持它们的顺序。

Second thing, Flask has something called url_for which helps you take an advantages of Jinja template and instead of hardcoding the html's page name you can refer to respective functions' name and pass a specific variables (You do that as shown below - in places where you have reference or action you place in href = "{{ url_for(" name of the function ") }}"第二件事,Flask 有一个叫做url_for的东西,它可以帮助您利用 Jinja 模板,而不是硬编码 html 的页面名称,您可以引用各个函数的名称并传递特定的变量(如下所示 - 在您有您在 href = "{{ url_for(" function 的名称") }}"中放置的参考或操作

In this example since you want to get to a specific record - point a specific value for each record.在此示例中,由于您想要获取特定记录 - 为每条记录指定一个特定值。 I placed row["id"] which refers to specific unique value for this record in the table (I assume you have some column that has unique values for every row - that's what should be placed in url_for).我放置了row["id"] ,它指的是表中该记录的特定唯一值(我假设您有一些列对每一行都有唯一值 - 这应该放在 url_for 中)。

Then in the Flask function ct_edit which is responsible for displaying specific row you've choosen by clicking an anchor tag, should receive that unique value as an argument(place it in route as well so that the records would be distinguishable).然后在Flask function ct_edit中负责显示您通过单击锚标记选择的特定行,应该接收该唯一值作为参数(也将其放置在路由中,以便记录可区分)。

<a href="{{ url_for("ct_edit", record_id = row["id"]) }}"><input type="submit" name="edit_btn" value="update"rel="tooltip" class="btn btn-info">
        <i class="material-icons"></i></input></a>

@app.route('/ct_edit/<int:record_id>')
def ct_edit(record_id):
    print('ct_edit')
    # the value of the first column of the selected row => ID
    # do something with the ID value and get a result
    return render_template('ct_edit.html', result=result) 

I hope I managed to brighten thigs for you a little bit.我希望我设法为你照亮了一点。

Keep well:)好好养:)

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

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