Python Flask避免在动态表上发布return to render temp

2024-09-29 08:17:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个使用flask的python应用程序,其核心路径如下:

@app.route('/test_limit', defaults={'page':1})
@app.route('/test_limit/page/<int:page>')
def test_results_limit(page):
    perpage=10
    startat=page*perpage
    results = []
    cursor = db.cursor()
    cursor.execute('SELECT * from pi_fb_limit limit %s, %s;', (startat,perpage))
    table = list(cursor.fetchall())
    return render_template('results_limits.html', table=table)

@app.route('/infringement/FB/<int:id>')
def infringement(id):
    cursor = db.cursor()
    cursor.execute('UPDATE p_test_search SET infringement = ''TRUE'' WHERE ID = %s', (id))  
    db.commit()
    return render_template('results_limits.html')

在我的HTML文件“result_限制.html“我有一个HTML代码迭代MySQL资源:

^{pr2}$

一切正常,但我的问题是,当上面的按钮正常呼叫路线时@应用程序路径('/involution/FB/'),浏览器被重定向到 结果_限制.html. 相反,我希望避免任何重定向,并保持在同一页上,每一行按钮都在创建post(更新记录)。在

有什么建议吗? 谢谢你 雷格斯 SL公司


Tags: test路径idapp应用程序dbhtmlpage
1条回答
网友
1楼 · 发布于 2024-09-29 08:17:48

您需要使用JavaScript提交数据,而无需重新加载页面。在

比如:

function submit_infringement(id) {
  this.removeAttribute('onclick');  // Prevent sumbitting twice.
  var x = new XMLHttpRequest();
  x.open('GET', '/infringement/FB/' + id, true);
  x.onload = function() {
    this.textContent = 'Infringement report sent!';
  }
  x.onerror = function(error) {
    (console.error || console.log)(error);
    this.textContent = 'An error occured. Try again.';
    this.onclick = function() { sumbit_infringemet(id); }
  }
  this.textContent = 'Sending infringement report...';
  x.send(null);
}
^{pr2}$

相关问题 更多 >