如何在Django中删除“已检查”用户?

时间:2018-11-26 14:48:26

标签: javascript django python-3.x django-templates django-views

如何在Django中删除“已检查”用户?

在对堆栈溢出进行了回答之后,我得到了下面的代码。但是,我尝试通过选中选中的用户来从数据库中删除选中的用户,然后使用Ajax按Delete,但是当我按下Delete时,什么也没有发生, 在服务器端也不会得到POST

views.py

def delete_list (request):
    if request.is_ajax():
        selected_tests = request.POST['test_list_ids']
        selected_tests = json.loads(selected_tests)
        for i, test in enumerate(selected_tests):
            if test != '':
                User.objects.filter(id__in=selected_tests).delete()
    return HttpResponseRedirect('/about')

any.html

<body>
  <button id="btn1" class="btn btn-round delete-btn" data-toggle="modal">
    <i class="material-icons" action >delete</i>   Delete
  </button>

  <div class="table-container">
    <table id="fresh-table" class="table table-striped test-list">
      <thead class="thead-table-list">
        <tr>
          <th scope="col">
            <div class="form-check">
              <label class="form-check-label">
                <input class="form-check-input" id="checkall" type="checkbox" value="">
                <span class="form-check-sign">
                  <span class="check"></span>
                </span>
              </label>
            </div>
          </th>
          <th scope="col">Id</th>
          <th scope="col">username</th>
          <th scope="col">email</th>
        </tr>
      </thead>
      <tbody>
      {% for test in list_wait %}
        <tr data-id="{{ test.id }}">
          <td>
            <div class="form-check">
              <label class="form-check-label">
                <input class="form-check-input check-ele" type="checkbox" value="">
                <span class="form-check-sign">
                  <span class="check"></span>
                </span>
              </label>
            </div>
          </td>
          <td>{{ test.id}}</td>
          <td>{{ test.username}}</td>
          <td>{{ test.email}}</td>
        </tr>      
        {% endfor %}
      </tbody>
    </table>
  {% csrf_token %}
  </div>

  <script type='text/javascript'>
  $("#btn1").click(function(){
    var selected_rows=[];
    $('.test-list').find('tr').each(function(){
      var row=$(this);
      console.log(row.find('input[type="checkbox"]').is(':checked'));
      if (row.find('input[type="checkbox"]').is(':checked')) {
        console.log(row.attr('data-id'));
        selected_rows.push(row.attr('data-id'));
      };
    });

    var selected_rows = JSON.stringify(selected_rows);
    $.ajax({
      url: "{% url 'delete_list' %}",
      type: 'POST',
      data: {'test_list_ids': selected_rows,'csrfmiddlewaretoken': $("[name=csrfmiddlewaretoken]").val()},
    })
  });
  </script>
</body>

如果有人可以帮助我,我将非常感激。

0 个答案:

没有答案