热门标签:
Q:

Flask和JavaScript在删除前确认

我有flask应用程序发布pythonanywhere的问题。 这是一个仪表板,如果我点击发布的标题,它应该显示发布的详细信息。 我想在删除之前添加确认消息。 我怎样才能做到呢? 现在我尝试使用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 {

}
    });

});

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

原网址
A:

类似任务的替代方法:

我通过仅使用HTML和jinja通过模态删除post之前实现了一个确认步骤。

{% 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 %}

所有回答

共 1 条

author avatar

在Flask中包含js文件的工作方式不同:

`<script type="text/javascript" src="{{ url_for('static', filename='js/confirm.js') }}"></script>`

有了这个,你应该能够加载JS

相似问题