我有以下内容:
模型.py
class Person(models.Model):
full_name = models.CharField(...)
address = models.CharField(...)
views.py
def all_persons(request):
persons = Person.objects.all()
return render(
request,
'my_template.html',
{
'persons':persons
}
)
my_template.html
<table class="table table-striped">
<thead>
<tr>
<th>Person's Fullname</th>
</tr>
</thead>
<tbody>
{% for person in persons %}
<td>{{ person.full_name }}</td>
<td>{{ person.address }}</td>
好的,所以代码将在模板中显示人员的无序列表,当然我可以设法按人员的姓名或地址进行排序,我想让用户能够按任何字段进行排序,我希望通过模板中表格的每一列上的按钮来完成此操作。例如,full_name 顶部的按钮和地址顶部的按钮,如果按下其中任何一个,则应根据按下的按钮对列表进行排序。
非常感谢您的阅读,希望您能帮助我。
编辑:我使用 Twitter Boostrap 2,因此 tablesorter 和 DataTables JS 无法工作。
最佳答案
查看这个 HTML 表格的 JS 插件:Datatables 。带有排序、过滤、分页和大量插件。很容易上手,但你可能会认为它有点臃肿。
另一种方法是让 View 接受一个额外的参数(例如,表示字段的整数),按该字段对查询集进行排序并呈现模板。
关于python - 客户端排序查询集,Django + TwitterBoostrap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14574111/