【问题标题】:Flask and JavaScript Confirm Before DeletingFlask 和 JavaScript 在删除前确认
【发布时间】:2018-02-03 02:53:48
【问题描述】:

我在 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 %}

confirm.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 文件并执行。但它什么也没做。请帮忙。

【问题讨论】:

  • confirm.js 文件肯定被加载了吗?查看浏览器中的开发人员工具,看看在尝试加载它时是否收到 404 错误。如果您在单击删除按钮时保持开发人员工具窗格打开,您可能还会看到其他有用的错误消息。
  • 我看不到任何错误消息。它只是行不通。它不加载 confirm.js 文件。当我尝试在 html head 中输入 javascript 代码时,它说 typeerror cannot read $.
  • 你的代码中是否包含了 jQuery? $ 是一个 jQuery 函数。

标签: javascript python html flask pythonanywhere


【解决方案1】:

类似任务的替代方法:

在通过模态删除帖子之前,我仅使用 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 %}

【讨论】:

    【解决方案2】:

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

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

    有了这个你应该可以加载 JS

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-10-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多