[英]Flask retrieve data
I am populating a table and want to allow users to be able to delete data as they see fit. 我正在填充一个表,并希望允许用户删除他们认为合适的数据。 How can I retrieve the row values they want to delete server side? 如何检索他们要删除服务器端的行值? I am populating the table through a list as such: 我通过这样的列表填充表:
<tr>
<td> {{ key }} </td>
<td> {{ value[1] }}</td>
<td> {{ value[0] }}</td>
<td> {{ value[2] }}</td>
<td> {{ value[3] }}</td>
<td> {{ value[4] }}</td>
<td> <button id="remove" value="Delete" name="btn2">Delete</button><td>
</tr>
You can create a view that will take an id
as an argument and then create a link in a template that will point to that view; 您可以创建一个将id
作为参数的视图,然后在模板中创建一个指向该视图的链接。 or, even better, since you added a jquery
tag to your question, create an AJAX request. 甚至更好的是,由于您在问题中添加了jquery
标记,因此请创建AJAX请求。 I'd use a DELETE
HTTP request for that since RESTful way is pretty much better, but you can get away with either GET or POST. 我将为此使用DELETE
HTTP请求,因为RESTful方法要好得多,但是您可以摆脱GET或POST的困扰。
@app.route('/delete/<int:id>', methods=['GET', 'POST', 'DELETE'])
def delete_data(id=None):
if id is None:
return 'Please provide an object ID to delete!'
object_to_delete = db.session.query(Data).filter_by(id=id)
db.session.delete(object_to_delete)
db.session.commit()
And then, in the template, 然后,在模板中
<a href="{{ url_for('delete', id=key) }}">Delete!</a>
Please note that I provided the easiest (but not really secure and/or proper) way. 请注意,我提供了最简单(但并非真正安全和/或适当)的方式。 An AJAX request, while having a lot more code on the client-side :), would be really much better. 一个AJAX请求,尽管在客户端:)上有很多代码,实际上会好得多。 There's a Flask Mega-Tutorial article that covers AJAX. Flask Mega-Tutorial上有一篇有关AJAX的文章 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.