如何在Django中实现搜索和排序功能table
how to implement search and sorting function in Django table
我知道有Django-datatable,django_tables2,甚至django_filter可以对table进行搜索和排序。我尝试过使用 Django-datatable、django_tables2 甚至 django_filter,但其中 none 有效。我附上了我的模板代码。我正在使用下面的代码呈现两个不同的 tables,一个带有操作和状态列,而另一个没有这两列。
{% 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 实现搜索栏的编辑代码
我按照 link 中的教程进行操作,但是当我在搜索栏中输入学生 ID 时,会显示所有数据,但不会显示特定输入的数据。
模板.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})
在table正确显示一些数据后,还必须确保显示过滤器表单集。我通常使用这样的东西(在这种情况下使用 {% 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
我知道有Django-datatable,django_tables2,甚至django_filter可以对table进行搜索和排序。我尝试过使用 Django-datatable、django_tables2 甚至 django_filter,但其中 none 有效。我附上了我的模板代码。我正在使用下面的代码呈现两个不同的 tables,一个带有操作和状态列,而另一个没有这两列。
{% 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 实现搜索栏的编辑代码
我按照 link 中的教程进行操作,但是当我在搜索栏中输入学生 ID 时,会显示所有数据,但不会显示特定输入的数据。
模板.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})
在table正确显示一些数据后,还必须确保显示过滤器表单集。我通常使用这样的东西(在这种情况下使用 {% 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