提问人:GW99 提问时间:11/26/2021 更新时间:12/2/2021 访问量:784
如何使用python(flask/mysql)编辑/删除数据表服务器端?
How to edit/delete datatables server side using python (flask/mysql)?
问:
瓶
@app.route(“/ajaxfiletest”,methods=[“POST”,“GET”]) def ajaxfiletest(): 尝试: 游标 = mysql.connection.cursor(MySQLdb.cursors.DictCursor) if request.method == 'POST': draw = request.form['draw'] 行 = int(request.form['start']) rowperpage = int(request.form['length']) searchValue = request.form[“search[value]”] 打印(绘制) print(行) 打印(行页) print(搜索值)
## Total number of records without filtering
cursor.execute("select count(*) as allcount from ates")
rsallcount = cursor.fetchone()
totalRecords = rsallcount['allcount']
print(totalRecords)
## Total number of records with filtering
likeString = "%" + searchValue +"%"
cursor.execute("SELECT count(*) as allcount from ates WHERE tes1 LIKE %s OR tes2 LIKE %s ", (likeString, likeString))
rsallcount = cursor.fetchone()
totalRecordwithFilter = rsallcount['allcount']
print(totalRecordwithFilter)
## Fetch records
if searchValue=='':
cursor.execute("SELECT * FROM ates ORDER BY id_ates asc limit %s, %s;", (row, rowperpage))
testing = cursor.fetchall()
else:
cursor.execute("SELECT * FROM ates WHERE tes1 LIKE %s OR tes2 LIKE %s limit %s, %s;", (likeString, likeString, row, rowperpage))
testing = cursor.fetchall()
data = []
for row in testing:
data.append({
'id_ates': row['id_ates'],
'tes1': row['tes1'],
'tes2': row['tes2'],
'tes3': row['tes3'],
})
response = {
'draw': draw,
'iTotalRecords': totalRecords,
'iTotalDisplayRecords': totalRecordwithFilter,
'aaData': data,
}
return jsonify(response)
except Exception as e:
print(e)
finally:
cursor.close()
<script>
$(document).ready(function() {
$('#datatables').DataTable({
"processing": true,
"serverSide": true,
"ajax": {
"url": "{{ url_for('ajaxfiletest') }}",
"type": 'POST'
},
"columns": [
{"data": "id_ates"},
{"data": "tes1"},
{"data": "tes2"},
{"data": "tes3"},
]
});
} );
</script>
<div class="container" >
<div class="row" style="padding:50px;">
<p><h1>test edit delete server side</h1>
</div>
<div class="row" style="padding:50px;">
<div >
<table id='datatables' class='display' width='100%'>
<thead>
<tr>
<th>id</th>
<th>tes1</th>
<th>tes2</th>
<th>tes3</th>
<th>Edit | Delete</th>
</tr>
</thead>
</table>
</div>
</div>
</div>
如何使用python(flask/mysql)编辑/删除数据表服务器端?
答:
1赞
GW99
12/2/2021
#1
我可以使用下面的脚本删除表记录,只有当我在id_ates中输入数字时,例如 id_ates=5,但是如果我写 id_ates=id,它就不起作用......
(加载资源失败:服务器响应状态为 404 ())
// Delete Record from Database.
var table= $("#datatables").DataTable();
$("#datatables").on('click', '[id*=btnDelete]', function(){
var id = $(this).attr("id_ates");
if(confirm("Are you sure you want to delete?")) {
$.ajax({
"url": "{{url_for('delfiletest', id_ates=5)}}",
method:"POST",
data:{id_ates:id},
success:function(data) {
table.ajax.reload();
}
})
} else {
return false;
}
});
评论