讀者查詢

我們可以在顯示某位讀者的頁面,順便帶出這位讀者的借閱記錄,方便我們了解這位讀者在哪些時候借閱了哪些書藉。

修改 library/reader/views.py 中的 ReaderView 類別,新增第 5, 16 - 21 行

from log.models import Log
class ReaderView(LoginRequiredMixin, DetailView): # 檢視讀者 model = Reader def get_context_data(self, **kwargs): ctx = super().get_context_data(**kwargs) ctx['log_list'] = Log.objects.filter( reader=self.object ).order_by('-id').select_related('book') return ctx

修改檢視讀者的頁面範本檔 library/templates/reader/reader_detail.html,插入第 24 - 52 行將讀者的借閱紀錄補上:

<div class="col-md-8"> <div class="card"> <div class="card-header">借閱紀錄</div> <ul class="list-group"> {% for log in log_list %} <li class="list-group-item"> <div> <span class="badge"> {{ log.checkout|date:"Y/m/d H:i" }} <i class="fas fa-arrow-alt-circle-right"></i> </span> {% if log.returned %} <span class="badge"> {{ log.returned|date:"Y/m/d H:i" }} </span> {% else %} <span class="badge badge-warning">借閱中</span> {% endif %} </div> <div> <a href="{% url 'book_view' log.book.id %}"> {{ log.book.title }} </a> </div> </li> {% empty %} <li class="list-group-item"></li> {% endfor %} </ul> </div> </div>