我知道有Django-datatable,django_tables2,甚至django_filter来对表执行搜索和排序。我尝试过使用Django-datatable,django_tables2甚至django_filter,但它们都没有用。我已经附加了模板的代码。我使用下面的代码渲染两个不同的表,一个是action和status列,另一个是没有这两列。
{% if some %}
<table id="example" class="display" cellspacing="0" width="100%" border="1.5px">
<tr align="center">
<th style="font-family: Calibri" > Student ID </th>
<th style="font-family: Calibri" > Student Name </th>
<th style="font-family: Calibri" > Start Date </th>
<th style="font-family: Calibri" > End Date </th>
<th style="font-family: Calibri" > Action </th>
<th style="font-family: Calibri" > Status </th>
</tr>
{% for item in query_results %}
<tr align="center">
<td style="font-family: Calibri" > {{item.studentID}} </td>
<td style="font-family: Calibri" > {{item.studentName}} </td>
<td style="font-family: Calibri" > {{item.startDate|date:'d-m-Y'}} </td>
<td style="font-family: Calibri" > {{item.endDate|date:'d-m-Y'}} </td>
<td style="font-family: Calibri" >
{% if item.status == 'Approved' %}
{% else %}
<a href="{% url 'timesheet:edit' id=item.id status='a' %}"><button onclick="alert('timesheet accepted')">Approve</button></a> <a href="{% url 'timesheet:edit' id=item.id status='d' %}"><button onclick="alert('timesheet denied')")>Deny</button></a>
{% endif %}
</td>
<td style="font-family: Calibri" >
{% if item.status %}
{{item.status}}
{% else %}
Pending
{% endif %}
</td>
</tr>
{% endfor %}
</table>
{% else %}
<table id="example" class="display" cellspacing="0" width="100%" border="1.5px">
<tr align="center">
<th style="font-family: Calibri" > Student ID </th>
<th style="font-family: Calibri" > Student Name </th>
<th style="font-family: Calibri" > Start Date </th>
<th style="font-family: Calibri" > End Date </th>
</tr>
{% for item in query_results %}
<tr align="center">
<td style="font-family: Calibri" > {{item.studentID}} </td>
<td style="font-family: Calibri" > {{item.studentName}} </td>
<td style="font-family: Calibri" > {{item.startDate|date:'d-m-Y'}} </td>
<td style="font-family: Calibri" > {{item.endDate|date:'d-m-Y'}} </td>
</tr>
{% endfor %}
</table>
{% endif %}
这是我追求的搜索栏。 Search bar to find by studentID,studentName,startDate,endDate,status
最新:使用THIS LINK执行搜索栏的编辑代码
我按照此链接中的教程进行操作,但是当我在搜索栏中输入studentID时,所有数据都会显示,但不会显示特定的输入数据。
模板.html文件
<form method="get" action="">
<button type="submit" style="float: right;"> Search </button>
<input type="text" name="q" placeholder="Search" style="float: right;">
</form>
<br></br>
{% if timesheets %}
{% for timesheet in timesheets %}
<li>{{ timesheet.studentID }} - {{ timesheet.studentName }} - {{ timesheet.startDate }} - {{ timesheet.endDate }} - {{ timesheet.status }} </li>
{% endfor %}
{% else %}
//display all the data
{% endif %}
views.py
def search(request):
if 'q' in request.GET and request.GET['q']:
q = request.GET['q']
timesheets = Timesheet.objects.filter(studentID__icontains=q)
return render(request, 'timesheet/supervisor_list_timesheet.html',
{'timesheets': timesheets})
答案 0 :(得分:0)
表格正确显示某些数据后,您还必须确保显示过滤器表格集。我通常使用这样的东西(在这种情况下使用{% load bootstrap3 %}
{% if filter %}
<div class="col-sm-10">
<form action="" method="get" class="form form-inline">
{% bootstrap_form filter.form layout='inline' %}
{% bootstrap_button 'filter' %}
</form>
</div>
{% endif %}
如果过滤器套件被分配给模板上下文为filter
,则情况应为when using the example from django-tables2 filtering page