首页 > 解决方案 > 在 print() 上返回空查询集

问题描述

谁能帮我找出为什么这不起作用。

def return_bulk_books(request):
    book = request.POST.getlist('marked_delete')
    books = Books.objects.filter(id__in = book)
    print(books)

print(books)返回一个空的查询集,而不是按要求打印书籍中的所有项目。

这是模型。

<form method="post" action="{% url 'return_bulk_books' %}">
    {% csrf_token %}
    {% for message in messages %}
      <div class="container text-center text-success">{{ message }}</div>
<table class="layout container">
    <thead>
      <th>Book</th>
      <th>Reg No</th>
      <th>Date of issue</th>
      <th><input type="checkbox" id="select-all"></th>
      <th>Actions</th>
    </thead>
  {% for item in all_borrowed %}
    <tr>
      <td>{{ item.book_id.book_name }}</td>
      <td>{{ item.book_id.reg_no}}</td>
      <td>{{ item.issue_date }}</td>
      <td>
          <input type="checkbox" name="marked_delete" value="{{ item.pk }}">
      </td>
      <td name = 'pk'><a onclick="return confirm('This book will be returned to the library. Hit ok to confirm..')" href="{% url 'return_book' item.book_id.pk %}" >Return</a> </td>
      
    </tr>
  {% endfor %}
  </table>
    {% endfor %}
<div class="container text-right">
    <button class="btn btn-dark" id="return_all" hidden="hidden" onclick="return confirm('The selected items will be returned to the library. Are you sure about this???')">Return Selected</button>
  </div>
</form>

标签: pythondjango-views

解决方案


推荐阅读