【发布时间】:2022-01-04 07:19:59
【问题描述】:
烧瓶
@app.route("/ajaxfiletest",methods=["POST","GET"]) def ajaxfiletest(): 尝试: 游标 = mysql.connection.cursor(MySQLdb.cursors.DictCursor) 如果 request.method == 'POST': draw = request.form['draw'] 行 = int(request.form['start']) rowperpage = int(request.form['length']) searchValue = request.form["search[value]"] 打印(绘制) 打印(行) 打印(行页) 打印(搜索值)
## 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) 编辑/删除数据表服务器端?
【问题讨论】:
-
请澄清您的具体问题或提供其他详细信息以准确突出您的需求。正如目前所写的那样,很难准确地说出你在问什么。
-
如何在datatables服务器端添加编辑和删除按钮?
标签: mysql ajax flask datatables server-side