gpt4 book ai didi

javascript - Flask 和 JavaScript 删除前确认

转载 作者:行者123 更新时间:2023-11-30 21:13:50 26 4
gpt4 key购买 nike

我在使用 pythonanywhere 发布的 Flask 应用程序时遇到问题。这是一个仪表板,如果我单击帖子的标题,它应该会显示帖子的详细信息。我想在删除前添加确认消息。我怎样才能做到?现在我尝试使用 javascript,但它不起作用。以下是我的代码。

app.py

import MySQLdb as mysql
con = mysql.connect("host", "username", "password", "database")
con.ping(True)

@app.route('/deleteschool/<int:School_Id>')
def deleteschool(School_Id):
try:
if session.get('user'):
#con = mysql.connect()
cursor = con.cursor()
query_string = "DELETE FROM school WHERE School_Id = '{School_Id}'".format(School_Id=School_Id)
cursor.execute(query_string)
#cursor.execute("DELETE from school where School_Id=%s", (School_Id))
delete=cursor.fetchall()
if len(delete) is 0:

con.commit()
return redirect('/getSchoolList')
else:
return redirect('/error')


else:
return redirect('/error')
except Exception as e:

return redirect('/error')

school_page_showpost.html

<script src="/static/js/confirm.js"></script>
{% for row in schoolpost %}
<div id="titleOutput" class="two fields">
<div class="field">
<label>TITLE</label>
<textarea id="ckeditor1" class="cleditor" disabled="yes" readonly="yes" rows="1">{{row[1]}}</textarea></div></div>

<div id="contentsOutput" class="field">
<h6></h6>
<label>CONTENTS</label>

<textarea id="ckeditor2" class="cleditor" disabled="yes" readonly="yes" rows="10" cols="80">{{row[2]}}

</textarea>
<script>
var editor2=CKEDITOR.replace( 'ckeditor2' );
</script>
</div>

</div>
<div class="extra content">
<div class="right floated author">
<span class="right floated time">{{row[4]}}&nbsp;</span>
<span class="category">School board&nbsp;</span>
<span class="writer">
{{row[3]}}</span>
</div>
</div>

</div>

<br><br>
<ul class="actions pagination">
<li><a onclick="javascript : window.location = '/editschool/{{row[0]}}' " class="ui button">EDIT</a></li>
<li><button id="confirmDeleteSchool" type="submit" name="confirmDeleteSchool" class="ui button">DELETE</button></li>
<li><a href="/school" class="ui button" >LIST</a></li>
</ul>

{% endfor %}

确认.js

$(function(){
$('#confirmDeleteSchool').click(function(){
var confirm = confirm("Are you sure?");
if (confirm == true) {
$.ajax({
url: '/delteschool/{{row[0]}}',

success: function(response){
console.log(response);
},
error: function(error){
console.log(error);
}
});

} else {

}
});

});

如果我点击删除按钮,它应该加载 confirm.js 文件并执行。但它什么都不做。请帮忙。

最佳答案

类似任务的另一种方法:

我仅使用 HTML 和 jinja 在通过模态删除帖子之前实现了确认步骤。

{% block body %}
<h1>Dashboard <small> Welcome {{session.username}} </small></h1>
<a class="btn btn-success" href="/add_article">Add Article</a>
<hr>
<table class="table table-striped">
<tr>
<th>ID</th>
<th>Title</th>
<th>Author</th>
<th>Date</th>
<th></th>
<th></th>
</tr>
{% for article in articles %}
<tr>
<td>{{article.id}}</td>
<td>{{article.title}}</td>
<td>{{article.author}}</td>
<td>{{article.create_date}}</td>
<td> <a href="edit_article/{{article.id}}" class="btn btn-default pull-right"> Edit </a></td>
<td>

<!-- Button trigger modal -->
<button type="button" class="btn btn-danger" data-toggle="modal" data-target="#exampleModalCenter{{article.id}}">
Delete
</button>
<!-- Modal -->
<div class="modal fade" id="exampleModalCenter{{article.id}}">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLongTitle{{article.id}}">Deleting Post Permanently</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
<div class="modal-body">
<h3>{{article.title}}??</h3>
<p>Are you sure?</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>

<form action="{{url_for('delete_article', id=article.id)}}" method="post">
<input type="submit" value="Delete" class="btn btn-danger">
</form>
</div>
</div>
</div>
</div>


</td>
</tr>
{% endfor %}
</table>
{% endblock %}

关于javascript - Flask 和 JavaScript 删除前确认,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45874906/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com